MVC路由问题,404和不匹配的路由

时间:2011-08-11 19:31:23

标签: asp.net-mvc-3 asp.net-mvc-routing

我正在尝试在MVC3中设置与传统(SP 2007)系统匹配的路由方案。这些是我设置的路线:

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*", graphicNumber = @"\d*-\d*" });

routes.MapRoute("Default", "", new { controller = "Home", action = "Index", id = UrlParameter.Optional });

它大部分工作得很好。但是,当试图点击“graphicNames”路线时,我遇到了一个问题。如果我使用这个网址:

http://localhost:1234/1234/321/Index

它工作正常,我得到了GraphicName控制器上的Index操作。但是,如果我这样做:

http://localhost:1234/1234/321

我得到了404。

所有其他路线似乎按预期工作。

编辑:解决方案是为客户的路线添加约束,以便操作只是'action = @“[A-Za-z] *”

1 个答案:

答案 0 :(得分:1)

上面你有:

routes.MapRoute("graphicNames",
"{customerNumber}/{graphicNameId}/{action}/{id}", 
new {controller="GraphicName", action="Index", id=UrlParameter.Optional}, 
new {customerNumber = @"\d*",graphicNameId = @"\d*", action=@"\w*"});

然而在此之前你有

routes.MapRoute("customers",
"{customerNumber}/{action}", 
new {controller = "customer", action = "Index"}, 
new {customerNumber = @"\d*"});

基于您的网址只有两个参数 /321分之1234 将首先匹配客户路线。添加路径约束,该操作必须仅为alpha,或者将其移动到graphicNames路径下方,因为在路径匹配中订单非常很重要。