Does anyone have experiences in providing a custom MvcRouteHandler? In my application I'd like to implement a globalization-pattern like http://mydomain/en/about or http://mydomain/de/about.
有没有人有提供自定义MvcRouteHandler的经验?在我的应用程序中,我想实现像http:// mydomain / en / about或http:// mydomain / de / about这样的全球化模式。
As for persistance, I'd like to have a cookie read as soon as a request arrives and if there is a language setting in this cookie apply it (so a user arriving at http://mydomain/ would be transferred to http://mydomain/en/ for example). If there is no cookie present, I'd like to get the first language the browser supports, apply this one and store it in this cookie.
至于持久性,我想在请求到达后立即读取cookie,如果此cookie中有语言设置则应用它(因此到达http:// mydomain /的用户将被转移到http:/ / mydomain / en /例如)。如果没有cookie,我想获得浏览器支持的第一种语言,应用此语言并将其存储在此cookie中。
I guess this can't be done with the standard routing mechanism mvc provides in it's initial project template. In a newsgroup I got the tip to have a look at the MvcRouteHandler and implement my own. But its hard to find a sample on how to do that.
我想这不能用mvc在其初始项目模板中提供的标准路由机制来完成。在新闻组中,我得到了一个提示,看看MvcRouteHandler并实现我自己的。但很难找到如何做到这一点的样本。
Any ideas?
有任何想法吗?
2 个解决方案
#1
2
I don't believe a custom route handler is required for what you are doing.
我不相信您正在做的事情需要自定义路由处理程序。
For your "globalized" URIs, a regular MVC route, with a constraint that the "locale" parameter must be equal to "en", "de", etc., will do. The constraint will prevent non-globalized URIs from matching the route.
对于“全球化”URI,常规MVC路由具有“locale”参数必须等于“en”,“de”等的约束。约束将阻止非全球化URI匹配路由。
For a "non-globalized" URI, make a "catch-all" route which simply redirects to the default or cookie-set locale URI.
对于“非全局化”URI,创建一个“全包”路由,它只是重定向到默认或cookie设置的语言环境URI。
Place the "globalized" route above the "catch-all" route in Global.asax, so that "already-globalized" URIs don't fall through to the redirection.
将“全局化”路由放在Global.asax中的“catch-all”路由之上,以便“已经全局化”的URI不会落入重定向。
You would need to make a new route handler if you want a certain URI pattern to trigger something that is not an action on a controller. But I don't think that's what you're dealing with, here.
如果您希望某个URI模式触发某个不是控制器上的操作的东西,则需要创建一个新的路由处理程序。但我不认为这就是你要处理的问题。
#2
0
You should be able to do this with ASP.NET MVC's default template, I'm doing something similar. Just build your routes as {language}/{controller}/{action}/{id}
您应该能够使用ASP.NET MVC的默认模板执行此操作,我正在做类似的事情。只需将您的路线构建为{语言} / {controller} / {action} / {id}
Just set a default route that goes to a controller that checks for the language cookie, and redirects the user based on that cookie.
只需设置一个默认路由,该路由将转到检查语言cookie的控制器,并根据该cookie重定向用户。
#1
2
I don't believe a custom route handler is required for what you are doing.
我不相信您正在做的事情需要自定义路由处理程序。
For your "globalized" URIs, a regular MVC route, with a constraint that the "locale" parameter must be equal to "en", "de", etc., will do. The constraint will prevent non-globalized URIs from matching the route.
对于“全球化”URI,常规MVC路由具有“locale”参数必须等于“en”,“de”等的约束。约束将阻止非全球化URI匹配路由。
For a "non-globalized" URI, make a "catch-all" route which simply redirects to the default or cookie-set locale URI.
对于“非全局化”URI,创建一个“全包”路由,它只是重定向到默认或cookie设置的语言环境URI。
Place the "globalized" route above the "catch-all" route in Global.asax, so that "already-globalized" URIs don't fall through to the redirection.
将“全局化”路由放在Global.asax中的“catch-all”路由之上,以便“已经全局化”的URI不会落入重定向。
You would need to make a new route handler if you want a certain URI pattern to trigger something that is not an action on a controller. But I don't think that's what you're dealing with, here.
如果您希望某个URI模式触发某个不是控制器上的操作的东西,则需要创建一个新的路由处理程序。但我不认为这就是你要处理的问题。
#2
0
You should be able to do this with ASP.NET MVC's default template, I'm doing something similar. Just build your routes as {language}/{controller}/{action}/{id}
您应该能够使用ASP.NET MVC的默认模板执行此操作,我正在做类似的事情。只需将您的路线构建为{语言} / {controller} / {action} / {id}
Just set a default route that goes to a controller that checks for the language cookie, and redirects the user based on that cookie.
只需设置一个默认路由,该路由将转到检查语言cookie的控制器,并根据该cookie重定向用户。