是否可以在路由配置中使用通配符?
我想制作
http://localhost/documents-about-cats?animal_id=4&type_id=2
路线:
http://localhost/home/search?animal_id=4&type_id=2
和
http://localhost/browse-documents-about-cats?animal_id=4&page=1
路线:
http://localhost/browse/index?animal_id=4&page=1
但是!
http://localhost/
必须路由到:
http://localhost/home/index
我希望MVC 4路由映射器可以处理通配符,所以我可以做类似的事情:
routes.MapRoute(
"Search", // Route name
"documents-about-*", // URL with parameters
new {
controller = "Home",
action = "Search"
}
);
routes.MapRoute(
"Browse", // Route name
"browse-documents-about-*", // URL with parameters
new {
controller = "Browse",
action = "Index"
}
);
routes.MapRoute(
"Default", // Route name
"", // URL with parameters
new {
controller = "Home",
action = "Index"
}
);
但这些路线映射似乎不起作用?
答案 0 :(得分:0)
我刚刚遇到一个解决上述问题的帖子如下:
routes.MapRoute(
"Search", // Route name
"documents-about-{any}", // URL with parameters
new
{
controller = "Home",
action = "Search",
any = UrlParameter.Optional
}
);
routes.MapRoute(
"Browse", // Route name
"browse-documents-about-{any}", // URL with parameters
new
{
controller = "Browse",
action = "Index"
}
);
routes.MapRoute(
"Blank", // Route name
"", // URL with parameters
new
{
controller = "Home",
action = "Index",
any = UrlParameter.Optional
}
);
它似乎适用于所有需要的场景 - 任何人都可以确认这是正确的方法吗?