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

ASP.Net MVC路由映射出现HTTP Error 404.0-Not Found,求故障排查帮助

Hey there, fellow ASP.NET MVC newbie! Let's walk through the most common culprits behind that frustrating 404.0 error when setting up routing—since you mentioned the RouteConfig, we'll start right there.

1. Double-Check Your RouteConfig.cs Rules

First, compare your RouteConfig code to the standard default setup—this is the baseline that should work for most basic cases:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        // Ignore static resources to avoid routing conflicts
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        // Default MVC route
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

Key checks here:

  • Did you include routes.IgnoreRoute? Missing this can cause static files (like .js or .css) to trigger routing logic incorrectly.
  • Are your default values pointing to an existing controller/action? If you don't have a HomeController with an Index method, the root path will throw a 404.
  • If you added custom routes, did you place them before the default route? Routes match in order—generic routes at the bottom will override specific ones if they're out of order.

2. Verify Controller & Action Naming Rules

MVC is strict about naming conventions—miss these, and the router won't find your code:

  • Controller classes must end with Controller: For example, HomeController is valid, but Home or HomeCtrl will be invisible to routing (you only use Home in the URL).
  • Action methods must be public: Private/protected methods can't be accessed via routing (unless you're using advanced attribute routing tricks, which we can ignore for now).
  • Match route parameters to method parameters: If your route includes {id}, your action should accept an optional parameter like public ActionResult Index(int? id) or a required one like public ActionResult Index(int id) (you'll need to include the id in the URL if it's required).

3. Confirm Routing is Registered in Global.asax

Your RouteConfig rules won't do anything unless they're hooked up in the application startup. Check your Global.asax.cs for this line in the Application_Start method:

RouteConfig.RegisterRoutes(RouteTable.Routes);

If this is missing, your routing setup is completely inactive—no wonder you're getting a 404!

4. Check Development Server/IIS Settings

  • Visual Studio IIS Express: Right-click your project → Properties → Web. Make sure the "Start URL" points to a valid path (like /Home/Index) or select "Specific Page" and enter the correct route.
  • Full IIS Deployment: Ensure IIS has the ASP.NET MVC components installed, your application pool uses the correct .NET Framework version matching your project, and the "Default Document" settings aren't overriding your MVC routing (though MVC usually bypasses this, it's worth checking).

5. If Using Attribute Routing, Enable It

If you're experimenting with [Route] attributes on your actions/controllers, you need to enable attribute routing in RouteConfig first:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    // Enable attribute routing
    routes.MapMvcAttributeRoutes();
    // Default route still applies for non-attribute routes
    routes.MapRoute(...);
}

Also double-check that the path in your [Route] attribute matches what you're trying to access (e.g., [Route("Products/Details/{id}")] maps to /Products/Details/123).

If none of these fix it, feel free to paste your RouteConfig code and a snippet of your controller class—we can dig deeper!

内容的提问来源于stack exchange,提问作者Ryan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 07:10:32