I'm trying to setup a routing scheme in MVC3 that matches against a legacy (SP 2007) system. These are the routes I've setup:
routes.MapRoute("administration",
"Administration/{action}/{id}",
new { controller = "Administration", action = "Index", id = UrlParameter.Optional });
routes.MapRoute("workOrderSearch",
"WorkOrderSearch",
new {controller = "Home", action = "WorkOrderSearch"});
routes.MapRoute("customers",
"{customerNumber}/{action}",
new {controller = "customer", action = "Index"},
new {customerNumber = @"\d*"});
routes.MapRoute("graphicNames",
"{customerNumber}/{graphicNameId}/{action}/{id}",
new {controller="GraphicName", action="Index", id=UrlParameter.Optional},
new {customerNumber = @"\d*",graphicNameId = @"\d*", action=@"\w*"});
routes.MapRoute("workOrders",
"{customerNumber}/{graphicNameId}/{graphicNumber}/WorkOrder/{action}/{id}",
new { controller = "WorkOrder", action = "Index", id = UrlParameter.Optional },
new { graphicNameId = @"\d*", graphicNumber = @"\d*-\d*" });
routes.MapRoute("graphics",
"{customerNumber}/{graphicNameId}/{graphicNumber}/{action}",
new { controller = "Graphic", action = "Index", id = UrlParameter.Optional },
new { graphicNameId = @"\d*", graphicNu开发者_运维知识库mber = @"\d*-\d*" });
routes.MapRoute("Default", "", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
It mostly works just fine. However, when trying to hit the "graphicNames" route, I run into a problem. If I use this url:
http://localhost:1234/1234/321/Index
it works fine and I get to the Index action on the GraphicName controller. However, if I do this:
http://localhost:1234/1234/321
I get a 404.
All other routes appear to work as expected.
Edit: The solution was to add a constraint to the customer's route so that actions were only 'action=@"[A-Za-z]*"
Above you have:
routes.MapRoute("graphicNames",
"{customerNumber}/{graphicNameId}/{action}/{id}",
new {controller="GraphicName", action="Index", id=UrlParameter.Optional},
new {customerNumber = @"\d*",graphicNameId = @"\d*", action=@"\w*"});
however before that you have
routes.MapRoute("customers",
"{customerNumber}/{action}",
new {controller = "customer", action = "Index"},
new {customerNumber = @"\d*"});
which based on your url with only TWO parameters /1234/321 will match the customers route first. Either add a route constraint that action must be alpha only, or move this beneath your graphicNames route, since order is very important in route matching.
精彩评论