【发布时间】:2010-12-15 08:44:01
【问题描述】:
对于一个场景,我有一个 ASP.NET MVC 应用程序,其 URL 如下所示:
http://example.com/Customer/List
http://example.com/Customer/List/Page/2
http://example.com/Customer/List
http://example.com/Customer/View/8372
http://example.com/Customer/Search/foo/Page/5
这些网址是通过Global.asax.cs中的以下路由实现的
routes.MapRoute(
"CustomerSearch"
, "Customer/Search/{query}/Page/{page}"
, new { controller = "Customer", action = "Search" }
);
routes.MapRoute(
"CustomerGeneric"
, "Customer/{action}/{id}/Page/{page}"
, new { controller = "Customer" }
);
//-- Default Route
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Customer", action = "Index", id = "" }
);
这些都进展顺利,直到一个新的需求出现并想要从 URL 中删除关键字“客户”,使 URL 看起来像:
http://example.com/List
http://example.com/List/Page/2
http://example.com/List
http://example.com/View/8372
http://example.com/Search/foo/Page/5
编辑: 更正了示例链接,感谢@haacked。
我尝试添加新的MapRoutes 以仅采用{action} 并将默认控制器设置为客户。例如/
routes.MapRoute(
"CustomerFoo"
, "{action}"
, new { controller = "Customer", action = "Index" }
);
这似乎可行,但是现在由 Html.ActionLink() 生成的所有链接都很奇怪并且不再是 URL 友好的。
那么,这可以实现吗?我的方向是否正确?
【问题讨论】:
-
你删除了旧路由吗?
-
不,我将新路线放在原始路线之前。
标签: asp.net-mvc routing