ASP.NET MVC、本地化路由和用户的默认语言 [英] ASP.NET MVC, localized routes and the default language for the user
问题描述
我正在使用 ASP.NET MVC 本地化路由.因此,当用户访问英文站点时,它是 example.com/en/Controller/Action
,而瑞典站点是 example.com/sv/Controller/Action
.
I am using ASP.NET MVC localized routes. So when a user goes to the English site it is example.com/en/Controller/Action
and the Swedish site is example.com/sv/Controller/Action
.
我如何确保当用户进入网站时,他/她会直接使用正确的语言?我知道如何获得我想要的语言,这不是问题.我过去常常做的是将这种文化放入 RegisterRoutes
方法中.但是由于我的页面处于集成模式,我无法从 Application_Start 获取请求.
How do I make sure that when a user enters the site he/she comes to the correct language directly? I do know how to get the language I want, that is not the issue. The thing I used to do is that I put that culture into the RegisterRoutes
method. But because my page is in integrated mode I cannot get the request from Application_Start.
那么我应该如何从一开始就确保路线是正确的?
So how should I make sure that the route is correct right from the beginning?
推荐答案
这就是我要做的.
~~ 免责声明:伪代码~~
~~ Disclaimer : psuedo code ~~
global.asax
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{*favicon}",
new { favicon = @"(.*/)?favicon.ico(/.*)?" });
routes.MapRoute(
"Question-Answer", // Route name
"{languageCode}/{controller}/{action}", // URL with parameters
new {controller = "home", action = "index"} // Parameter defaults
);
}
注意:控制器和/或动作不需要是第一和第二.事实上,它们根本不需要存在,在url with parameters
部分.
Take note: the controller and/or action do NOT need to be first and second. in fact, they do not need to exist at all, in the url with parameters
section.
然后……
HomeController.cs
public ActionResult Index(string languageCode)
{
if (string.IsNullOrEmpty(languageCode) ||
languageCode != a valid language code)
{
// No code was provided OR we didn't receive a valid code
// which you can't handle... so send them to a 404 page.
// return ResourceNotFound View ...
}
// .. do whatever in here ..
}
奖金建议
您还可以添加路由约束您的路线,因此它只接受 languageCode
参数的某些字符串.所以偷这个伙计的代码 ....
Bonus suggestion
You can also add a Route Constraint to your route, so it only accepts certain strings for the languageCode
parameter. So stealing this dude's code ....
(更多伪代码)...
public class FromValuesListConstraint : IRouteConstraint
{
public FromValuesListConstraint(params string[] values)
{
this._values = values;
}
private string[] _values;
public bool Match(HttpContextBase httpContext,
Route route,
string parameterName,
RouteValueDictionary values,
RouteDirection routeDirection)
{
// Get the value called "parameterName" from the
// RouteValueDictionary called "value"
string value = values[parameterName].ToString();
// Return true is the list of allowed values contains
// this value.
return _values.Contains(value);
}
}
意味着你可以这样做......
means you could do this ......
routes.MapRoute(
"Question-Answer", // Route name
"{languageCode}/{controller}/{action}", // URL with parameters
new {controller = "home", action = "index"} // Parameter defaults
new { languageCode = new FromValuesListConstraint("en", "sv", .. etc) }
);
你有它:)
我为版本控制我的 MVC Api 做了这样的事情.
I do something like this for versioning my MVC Api.
GL :) 希望这会有所帮助.
GL :) Hope this helps.
这篇关于ASP.NET MVC、本地化路由和用户的默认语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!