You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visual Studio 2019运行ASP.NET Core项目时出现HTTP ERROR 404(本地页面无法找到)问题求助

Troubleshooting the 404 Error for /api/values in ASP.NET Core

Hey there, as someone who's worked through plenty of ASP.NET Core routing headaches, let's walk through the most likely causes for that 404 you're seeing:

1. Incorrect Routing Configuration

  • First, check your controller's route attributes. Your ValuesController should have something like this at the top:
    [ApiController]
    [Route("api/[controller]")]
    public class ValuesController : ControllerBase
    {
        // ... actions here
    }
    
    If the route is misspelled (like api/value instead of api/values) or missing the [ApiController] attribute, the framework won't map requests to it correctly.
  • Make sure routing is enabled in your startup code:
    • For .NET 6+ (top-level statements in Program.cs): Verify you have app.MapControllers(); after app.UseRouting();.
    • For older versions (Startup.cs): Check that app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); is present in the Configure method, and that app.UseRouting(); comes before it.

2. Missing or Misconfigured Controller

  • Double-check that your project actually contains a ValuesController.cs file, and that the class name ends with Controller (this is a required convention for ASP.NET Core to recognize it as a controller). If it's named ValueController (singular), the route would be /api/value, not /api/values.
  • Ensure the controller class is public and inherits from ControllerBase (for Web APIs) or Controller. Internal classes or ones without the base class won't be picked up by the framework.

3. Launch Configuration Discrepancies

  • Check the Visual Studio output window when you start the project to confirm the app is actually listening on https://localhost:44368 and http://localhost:5000. Sometimes port conflicts can cause the app to use a different port automatically, so you might be hitting the wrong address.
  • While your ASPNETCORE_ENVIRONMENT is set to Development, it's worth glancing at appsettings.Development.json to ensure there aren't any unexpected routing-related configurations overriding defaults (though this is less common).

4. Middleware Order Issues

  • Middleware execution order is critical in ASP.NET Core. Make sure your middleware pipeline follows this general order:
    app.UseHttpsRedirection();
    app.UseRouting();
    // Other middleware (like authentication) goes here
    app.UseAuthorization();
    app.MapControllers(); // This must come after UseRouting and UseAuthorization
    
    If MapControllers() is placed too early or late, requests might not reach the routing system.

5. Incorrect Startup Project

  • Confirm that xxxxxxx.Web.API is set as the startup project in Visual Studio. It's easy to accidentally select a class library or other project instead, which won't host the Web API and will result in a 404 when you try to access the endpoint.

6. Action Method Route Mismatch

  • If your ValuesController doesn't have an action that maps to a GET request at /api/values, you'll get a 404. Make sure you have an action like this:
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new[] { "value1", "value2" };
    }
    
    If all your actions have specific route templates (like [HttpGet("get-by-id/{id}")]) but no default [HttpGet], there's no action to handle the base /api/values request.

7. Compilation or Cache Issues

  • Check the Visual Studio error list for any compilation errors. If the project fails to build, it might not start correctly, leading to a 404.
  • Try cleaning your solution (right-click solution > Clean), deleting the bin and obj folders in the Web.API project, then rebuilding. Stale build artifacts can sometimes cause unexpected routing problems.

内容的提问来源于stack exchange,提问作者Tofik al-Radi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 08:17:42