我的MVC4中有以下路线......
routes.MapRoute("Account", "Account/{action}", new { controller = "Account", action = "Index" });
routes.MapRoute("Admin", "Admin/{action}", new { controller = "Admin", action = "Index" });
routes.MapRoute("Custom", "{action}", new { controller = "Home", action = "Index" });
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
...并且需要找到一种方法将所有网址中包含2
符号(www.domain.com/word2pdf
)的网址路由到操作索引并将网址路径(word2pdf
)传递为{{ 1}}参数。
api
任何想法如何做到这一点?
答案 0 :(得分:2)
正如@Murali所说,正确的方法是使用属性路由,但这只是mvc5的新功能。
如果你想要或必须留在mvc4:
routes.MapRoute(name: "SomeRoutingName",
url: "{api}",
defaults: new { controller = "SomeControllerName", action = "Index"},
constraints: new { api= ".+2.+"});
也适用于某些lol,这也应该有效:
routes.MapRoute(name: "SomeRoutingName",
url: "{partA}2{partB}",
defaults: new { controller = "SomeControllerName", action = "Index" });
在控制器中:
public ActionResult Index(string partA, string partB)
{
var api = string.Concat(partA,"2",partB);
}