2013-04-22 73 views
2


我想做的事情做在MVC几个地图路由都喜欢相同。
注册在MVC asp.net多途径

本地主机:1010/ABCD /家/索引
本地主机:1010 /家庭/索引/ ABCD

ID = ABCD控制器=家行动=指数

我用波纹管的代码,但它不” t工作

public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(
      "ShoppingManagment", 
      "{id}/{controller}/{action}", 
      new { controller = "ShoppingManagment", 
      action = "ShoppingManagment", id = UrlParameter.Optional }); 


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

    } 
+0

你想要这些路线匹配多个控制器或只是ShoppingManagement吗?你的问题是,目前这两个路由定义是相同的 - string/string/string,所以它们都会被顶层路由拾取。 – Richard 2013-04-22 05:29:30

回答

11

它不会工作,因为两个路由具有相同的格式。

所以MVC路由引擎不能同时URL模式进行区分。

尝试直接写入控制器进入URL模式。

public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(
      "ShoppingManagment", 
      "{id}/ShoppingManagment/{action}", 
      new { controller="ShoppingManagment", action = "ShoppingManagment", id = UrlParameter.Optional }); 


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

    } 
+0

plz检查更新。 – Virus 2013-04-22 05:35:08

+0

我想使用第一种格式的特殊控制器和其他控制器使用第二种格式。我该怎么做? – Ahmad 2013-04-22 05:36:46

+0

是的,通过使用固定的控制器在url模式中的第一条路线,你可以实现这一点...看到代码在答案中有“S​​hoppingManagement”控制器到url模式,因此无论哪个网址将ShoppingManagement作为第二个参数匹配此路线,其他人将匹配其他路线。 – Virus 2013-04-22 05:40:28