How to fix ASP.NET Core WEB API ? localhost error

LOGAN PANUCAT 0 Reputation points
2025-02-12T13:32:29.9233333+00:00

I am trying to run an API test first but then whenever i tried to run it it gives me this error localhost cannot be found even on the localhost:5125 still wont run User's image

User's image

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,790 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
387 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Martin Brandl 545 Reputation points MVP
    2025-02-12T13:55:14.88+00:00

    Usually, you will have to call a controller in order to get a response back. Also, if you are using swagger, you should be able to access the swagger ui using https://localhost:7211/swagger

    If you still get a 404, you should share the code of the controller as well as the startup code so that we can help you.

    0 comments No comments

  2. AgaveJoe 29,866 Reputation points
    2025-02-12T15:51:16.2333333+00:00

    If this is an ASP.NET Core 9 Web API project and you did not configure a root route then a 404 is expected. Try accessing one of your HTTP GET actions or setup a root route.

        [Route("api/[controller]")]
        [ApiController]
        public class ValuesController : ControllerBase
        {
            private ILogger<ValuesController> _logger;
            public ValuesController(ILogger<ValuesController> logger)
            {
                _logger = logger;
            }
    
            [HttpGet("/")]
            public IActionResult Get()
            {
                _logger.LogInformation("Hello from the root route.");
                return Ok(new { message = "Hello from the root route." });
            }
        }
    
    

    If this is an If this is an ASP.NET Core 9 Web API project and you expect to see a documentation GUI in the browser (Open API) and have not configured one then see the official documentation.

    https://learn.microsoft.com/en-us/aspnet/core/fundamentals/openapi/aspnetcore-openapi?view=aspnetcore-9.0&tabs=visual-studio

    0 comments No comments

  3. SurferOnWww 3,886 Reputation points
    2025-02-13T01:31:42.52+00:00

    I guess you have created your Web API app with the target framework of .NET 9.0. Your app is probably working and will return JSON string as expected when you request the WeatherForecast as shown below:

    enter image description here

    0 comments No comments

  4. Ping Ni-MSFT 4,890 Reputation points Microsoft External Staff
    2025-02-13T02:06:24.7+00:00

    Hi @LOGAN PANUCAT,

    404 means the requested url cannot be found on the server. You need check if your app actually contains such endpoint with requested route /.

    Note: If you use default WEB API project template, there is no route for / which is quite different with MVC Project.

    Default WEB API project with target framework .net 8 or lower, it will integrate with Swagger, you can access the url:https://localhost:7211/swagger/index.html.

    Default WEB API project with target framework .net 9, it will integrate with OpenAPI, you can access the url:http://localhost:7211/openapi/v1.json.

    Only if you define the endpoint like the following examples, you can access the url / .

    For Example:

    Open your Program.cs (or Startup.cs if using an older version) and check if you have the following code.

    app.MapGet("/", () => "Hello, World!");
    

    Or check your controller if define the route like below:

    [ApiController]
    [Route("api/[controller]")]  // This is the default route for this controller
    public class TestController : ControllerBase
    {
        [HttpGet("/")]  // This explicitly maps to the root `/`
        public IActionResult Root() => Ok("Welcome to my API!");
        [HttpGet] // This will respond to `/api/test`
        public IActionResult Get() => Ok("This is the Test API.");
    }
    
    

    More details you could refer to the official document for better understanding:

    Get Started with ASP.NET Core WEB API

    Attribute routing for REST APIs


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Rena

    0 comments No comments

Your answer

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