如何在MVC选择路由之前添加路由参数
我正在尝试向现有 MVC 3 应用程序添加对不同语言的支持。到目前为止,我的链接是
oldUrl -> myapp.com/Item/Details/5/some-title
并且有来自网络上不同位置的网站链接。但现在我想更改 URL,以便包含语言:
newUrl -> kmyapp.com/en/Item/Details/5/some-title
但是我希望 oldUrl 链接有效。问题是我该如何做到这一点...最简单的方法是添加另一条路线,但我已经有太多路线,而且它变得有点丑陋,当我从一个不存在的页面创建网址时有语言,链接也没有语言,所以我必须做其他事情。
另一种方法是覆盖 MVC 尝试将请求映射到特定路由之前的内容,但已经解析了请求 url 并填充了 RouteValues 字典。所以我可以检查语言,如果它没有包含在路由参数中,我可以自己添加它并调用父类的实现来继续。这可能吗?我应该看什么 - UrlRoutingModule,MvcHttpHandler 还是其他?我可以覆盖什么以及如何告诉 MVC 使用我的版本?
任何其他想法也将不胜感激!
I am trying to add support for different languages to existing MVC 3 application. So far my links were
oldUrl -> myapp.com/Item/Details/5/some-title
And there are links to the website from different places on the web. But now I want to change the URL so the language is included:
newUrl -> kmyapp.com/en/Item/Details/5/some-title
However I want the oldUrl links to be valid. And the question is how do I do this... The easiest way would be to add another route, but I already have too many of them and it is getting kind of ugly, and when I create urls from a page that doesn't have the language the links also don't have the language so I have to do something else then.
Another way would be to override something that stands just before MVC tries to map the request to a specific route, but has already parsed the request url and filled the routeValues dictionary. So I can just check for the language and if it is not included in the route parameters I can add it on my own and call the implementation of the parent class to continue. Is that possible? What should I look at - UrlRoutingModule, MvcHttpHandler or something else? What can I override and how to tell MVC to use my version?
Any other ideas would be also appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然有很多解决方案。我将向您展示两个:
解决方案一 - Asp.net MVC 路由
提供涵盖旧和新的路由路由:
如您所见,我也为旧路由提供了默认语言,因为它不存在于 URL 中。无论您是否愿意,都是您自己的决定,但这种路由可以不重复您的控制器操作。在任何情况下,您都必须定义可以通过这种方式提供的默认语言。
还有一个更大的问题是您是否仍然想要支持旧的 URL,或者您宁愿重定向它们(HTTP 重定向永久状态 301)。
永久重定向
将旧路由更改为:
然后编写一个执行重定向的控制器类:
解决方案二 - IIS URL 重写模块
该解决方案依赖于 IIS URL 重写模块,您可以在其中将任何请求重写为您首选的默认值,而无需选择语言。
我不打算在这里写 URL 重写的工作原理,因为有大量的网络资源提供了相关的详细信息。
There are of course many solutions. I'm going to show you two:
Solution one - Asp.net MVC routing
Provide routing that covers old and new routing:
As you can see I've provided language default for the old routes as well since it's not present in the URL. Whether you want that or not is your own decision but this kind of routing makes it possible to not duplicate your controller actions. In any case you'd have to define a default language which can be provided this way.
There's a bigger question whether you still want to support old URLs or you'd rather redirect them (HTTP Redirect Permanent Status 301).
Permanent redirection
Change old routes to:
Then write a controller class that does redirection:
Solution two - IIS URL rewriting module
This solution relies on IIS URL Rewriting module, where you could rewrite any requests without language selection to your preferred default.
I'm not going to wrote how URL Rewriting works here, because there're plenty of web resources with detailed info about that.
另一种方法是覆盖 MVC 尝试将请求映射到特定路由之前的内容,但它已经解析了请求 url 并填充了routeValues 字典。所以我可以检查语言,如果它没有包含在路由参数中,我可以自己添加它并调用父类的实现来继续。 这可能吗?
您可以覆盖 GetRouteData在 RouteBase 中,其中包含 URL 详细信息。
Another way would be to override something that stands just before MVC tries to map the request to a specific route, but has already parsed the request url and filled the routeValues dictionary. So I can just check for the language and if it is not included in the route parameters I can add it on my own and call the implementation of the parent class to continue. Is that possible?
You can Override GetRouteData in RouteBase which will have URL details in it.