Search code examples
asp.net-mvcasp.net-mvc-routing

MVC routing error 404


What am I doing wrong?

My default /User route

  routes.MapRoute(
            "User", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new {controller = "User", action = "Index", id = UrlParameter.Optional} // Parameter defaults
            );

I want to separate code so I create another controller "UserProducts"

my route

 routes.MapRoute(
            "UserProducts", // Route name
            "user/products/{action}/{id}", // URL with parameters
            new { controller = "UserProducts", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

I have ActionResult Index in my UserProducts controller, but however my

localhost/user/products

doesn't work:

Error 404 - The resource cannot be found.

Solution

  • You probably have them in the wrong order. The order in which you register these routes is significant, and the first mapping will override the ones after it. Put the UserProducts line above the one for User.