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:
How to fix ASP.NET Core WEB API ? localhost error
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
Developer technologies ASP.NET ASP.NET Core
Developer technologies ASP.NET ASP.NET API
4 answers
Sort by: Most helpful
-
-
Anonymous
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
(orStartup.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
-
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.
-
AgaveJoe 30,126 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.