Verify the code

Peter_1985 3,071 Reputation points
2026-01-27T06:12:19.0966667+00:00

Hi,

I deployed the code below to the server. How to verify it is running well?

User's image

Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Jack Dang (WICLOUD CORPORATION) 11,065 Reputation points Microsoft External Staff Moderator
    2026-01-27T08:26:17.73+00:00

    Hi @Peter_1985 ,

    Thanks for reaching out.

    Here are a few practical ways to test it:

    1. Test the endpoint directly

    The easiest way is to send a test request to your API. You can use tools like Postman, curl, or even your browser's developer tools to send a POST request to https://your-server.com/api/uploadfile with a file attached.

    https://learn.microsoft.com/en-us/aspnet/core/test/http-files

    If everything's working, you should get back an "Ok" response with your success message. If you send a request without a file, you should get a "BadRequest" response with "File not selected".

    2. Check your server's file system

    After sending a test file, navigate to the "Uploads" folder on your server (relative to where your application is running) and verify the file actually saved there.

    3. Review application logs

    Check your application logs for any errors or warnings. ASP.NET Core has built-in logging that can help you spot issues.

    https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/

    You can also add some logging to your controller to track when files are being uploaded successfully or when errors occur.

    4. Set up health checks (optional but recommended)

    For ongoing monitoring, consider implementing health checks in your ASP.NET Core app.

    https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/health-checks

    This lets you have a dedicated endpoint that reports whether your application is healthy and running.

    Quick tip:

    Test both scenarios - sending a valid file AND trying without a file - to make sure both your happy path and error handling work as expected.

    Hope this helps! If my answer was helpful - kindly follow the instructions here so others with the same problem can benefit as well.


  2. SurferOnWww 5,261 Reputation points
    2026-01-27T12:48:38.2866667+00:00

    // Save the file to a server loaction

    I guess that the Directory.GetCurrentDirectory method does not work to obtain the physical path at the server location and that is your issue. If so, please consider the following:

    using Microsoft.AspNetCore.Mvc;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Http;
    using System.IO;
     
    namespace WebAPI.Controllers
    {
        [ApiController]
        [Route("[controller]")]
        public class FileUpDownloadController : ControllerBase
        {
            // DI required to obtain physical path at the server
            private readonly IWebHostEnvironment _hostingEnvironment;
     
            public FileUpDownloadController(IWebHostEnvironment hostingEnvironment)
            {
                this._hostingEnvironment = hostingEnvironment;
            }
     
            [HttpPost]
            public async Task<IActionResult> PostFile(IFormFile postedFile)
            {
                string result = "";
                if (postedFile != null && postedFile.Length > 0)
                {
                    string filename = Path.GetFileName(postedFile.FileName);
     
                    // Get physical path of application route at the server
                    // As for the path in wwwroot, use WebRootPath property
                    string contentRootPath = _hostingEnvironment.ContentRootPath;
                    string filePath = contentRootPath + "\\" + 
                                      "UploadedFiles\\" + filename;
     
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await postedFile.CopyToAsync(stream);
                    }
     
                    result = filename + " (" + postedFile.ContentType +
                             ") - " + postedFile.Length +
                             " bytes uploaded";
                }
                else
                {
                    result = "upload fail";
                }
     
                return Content(result);
            }
        }
    }
    
    0 comments No comments

  3. AgaveJoe 30,891 Reputation points
    2026-01-27T15:39:22.6433333+00:00

    It is difficult to provide a verification plan without knowing the current state of your deployment. To help us troubleshoot, please let the community know if you have already tried calling the endpoint and whether you encountered specific errors.

    Keep in mind that the snippet provided is a basic proof-of-concept. For a production-ready service, you'll want to ensure you've implemented:

    Authorization: To secure the endpoint.

    Validation: To prevent oversized or malicious file uploads.

    Additionally, based on your recent posts, it’s unclear if this code is hosted as a Web API or if you are attempting to use it as a library within a client app. Clarifying the calling mechanism would allow us to give you a much better answer


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.