如何在 ASP.NET MVC 中拥有小写路由?
如何在 ASP.NET MVC 中使用小写字母(如果可能的话加上下划线)的路由? 这样我就可以让 /dinners/details/2
调用 DinnersController.Details(2)
,如果可能的话,/dinners/more_details/2
调用 DinnersController.MoreDetails(2)
?
所有这一切同时仍然使用像 {controller}/{action}/{id}
这样的模式。
How can I have lowercase, plus underscore if possible, routes in ASP.NET MVC? So that I would have /dinners/details/2
call DinnersController.Details(2)
and, if possible, /dinners/more_details/2
call DinnersController.MoreDetails(2)
?
All this while still using patterns like {controller}/{action}/{id}
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用 System.Web.Routing 4.5,您可以通过设置 RouteCollection 的 LowercaseUrls 属性来实现此简单操作:
同时假设您这样做是出于 SEO 原因,您希望将传入 url 重定向为小写(如本文中的许多链接所述)。
With System.Web.Routing 4.5 you may implement this straightforward by setting LowercaseUrls property of RouteCollection:
Also assuming you are doing this for SEO reasons you want to redirect incoming urls to lowercase (as said in many of the links off this article).
当我想做同样的事情并且工作良好时,这两个教程很有帮助:
http://www.coderjournal.com/2008/03/force-mvc-route-url-lowercase/
http://goneale.com/2008/12/ 19/lowercase-route-urls-in-aspnet-mvc/
编辑:对于有区域的项目,您需要修改 GetVirtualPath() 方法:
These two tutorials helped when I wanted to do the same thing and work well:
http://www.coderjournal.com/2008/03/force-mvc-route-url-lowercase/
http://goneale.com/2008/12/19/lowercase-route-urls-in-aspnet-mvc/
EDIT: For projects with areas, you need to modify the GetVirtualPath() method:
如果您碰巧使用 ASP.NET Core,您可能应该看看这个:
If you happened to use ASP.NET Core, you probably should have a look at this:
如果您使用 UrlHelper 生成链接,则只需将操作和控制器的名称指定为小写:
结果为:/media/photos/delete/64(即使我的控制器和操作是 pascal 大小写)。
If you are using the UrlHelper to generate the link, you can simply specify the name of the action and controller as lowercase:
Results in: /media/photos/delete/64 (even though my controller and action are pascal case).
我在 Nick Berardi 的 Coder Journal 中找到了这个,但是它没有有关如何实现
LowercaseRoute
类的信息。 因此在此处重新发布并提供附加信息。首先将
Route
类扩展为LowercaseRoute
然后修改 Global.asax.cs 的
RegisterRoutes
方法但是我想知道一种使用路由的方法.地图路线...
I found this at Nick Berardi’s Coder Journal, but it did not have information on how to implement the
LowercaseRoute
class. Hence reposting here with additional information.First extend the
Route
class toLowercaseRoute
Then modify the
RegisterRoutes
method of Global.asax.csI would however like to know a way to use routes.MapRoute...
您可以通过添加此类作为 RouteCollection 的扩展来继续使用 MapRoute 语法:
现在您可以在应用程序的启动中使用“MapRouteLowerCase”而不是“MapRoute”:
You can continue use the MapRoute syntax by adding this class as an extension to RouteCollection:
Now you can use in your application's startup "MapRouteLowerCase" instead of "MapRoute":
这实际上有两个答案:
不过,你得靠自己的下划线......
This actually has two answers:
You're on your own for the underscores, though...
您可以使用 ActionName 属性吗?
我认为案件并不重要。 URL 中的 More_Details、more_DETAILS、mOrE_DeTaIL 都会将您带到同一个控制器操作。
Could you use the ActionName attribute?
I don't think case matters. More_Details, more_DETAILS, mOrE_DeTaILs in the URL all take you to the same Controller Action.