关于Mvc管线

本文关键字:管线 Mvc 关于 | 更新日期: 2023-09-27 18:00:12

我只想为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也可以打开。这不是我想要的。有人能帮我吗?

关于Mvc管线

我认为您缺少约束:

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的routedebug。你可以用nuget得到它。