我们说我有两套类似的控制器动作:
Photos/OriginalList/{filterBy}
Photos/PhotoShopList/{filterBy}
Photos/OriginalSingle/{id}/{filterBy}
Photos/PhotoShopSingle/{id}/{filterBy}
在这两个组中,filterBy是可选的。
有没有办法创建可以将类似操作组合在一起的路由,还是我需要为每个操作创建显式路由?
我尝试过类似的东西,但它不起作用:
routes.MapRoute(name: "FilteredPhotoList", url: "Photos/{action=*List}/{filterBy}", defaults: new {
controller = "Photos",
action = "OriginalList",
filterBy = UrlParameter.Optional
}, namespaces: new string[] { "BetterExample" });
routes.MapRoute(name: "FilteredPhotoSingle", url: "Photos/{action=*Single}/{id}/{filterBy}", defaults: new {
controller = "Photos",
action = "OriginalSingle",
filterBy = UrlParameter.Optional
}, namespaces: new string[] { "BetterExample" });
答案 0 :(得分:1)
我会明确定义每条路由,以提高可读性和代码清洁度。