关于Mvc路线

时间:2015-06-29 09:20:15

标签: c# asp.net-mvc routes

我想仅为defaultController

设置规则
 routes.MapRoute(
     name: "testDefault",
     url: "Default/{action}/{id}.html",
     defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional }
 );
 routes.MapRoute(
     name: "Default",
     url: "{controller}/{action}/{id}",
     defaults: new { 
          controller = "Test", 
          action = "Show", 
          id = UrlParameter.Optional 
     }
);

但是,http://10.12.2.37:120/test/show/2.html也可以打开。这不是我想要的。有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

我认为你缺少限制:

routes.MapRoute(
     name: "testDefault",
     url: "Default/{action}/{id}.html",
     defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional },
     constraints: new {
          id = @"\d+"
     }
 );
 routes.MapRoute(
     name: "Default",
     url: "{controller}/{action}/{id}",
     defaults: new { 
          controller = "Test", 
          action = "Show", 
          id = UrlParameter.Optional 
     },
     constraints: new {
          id = @"\d+"
     }
);

我还建议您使用来自Phil Haack的路由测试工具,例如routedebugger。您可以使用nuget获取它。