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
ValuesControllershould have something like this at the top:
If the route is misspelled (like[ApiController] [Route("api/[controller]")] public class ValuesController : ControllerBase { // ... actions here }api/valueinstead ofapi/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 haveapp.MapControllers();afterapp.UseRouting();. - For older versions (Startup.cs): Check that
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });is present in theConfiguremethod, and thatapp.UseRouting();comes before it.
- For .NET 6+ (top-level statements in
2. Missing or Misconfigured Controller
- Double-check that your project actually contains a
ValuesController.csfile, and that the class name ends withController(this is a required convention for ASP.NET Core to recognize it as a controller). If it's namedValueController(singular), the route would be/api/value, not/api/values. - Ensure the controller class is
publicand inherits fromControllerBase(for Web APIs) orController. 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:44368andhttp://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_ENVIRONMENTis set toDevelopment, it's worth glancing atappsettings.Development.jsonto 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:
Ifapp.UseHttpsRedirection(); app.UseRouting(); // Other middleware (like authentication) goes here app.UseAuthorization(); app.MapControllers(); // This must come after UseRouting and UseAuthorizationMapControllers()is placed too early or late, requests might not reach the routing system.
5. Incorrect Startup Project
- Confirm that
xxxxxxx.Web.APIis 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
ValuesControllerdoesn't have an action that maps to aGETrequest at/api/values, you'll get a 404. Make sure you have an action like this:
If all your actions have specific route templates (like[HttpGet] public IEnumerable<string> Get() { return new[] { "value1", "value2" }; }[HttpGet("get-by-id/{id}")]) but no default[HttpGet], there's no action to handle the base/api/valuesrequest.
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
binandobjfolders in the Web.API project, then rebuilding. Stale build artifacts can sometimes cause unexpected routing problems.
内容的提问来源于stack exchange,提问作者Tofik al-Radi
相关产品推荐
相关产品推荐

