我需要获取语言参数的值,我尝试了以下代码:
if(HttpContext.Current.Request.RequestContext.RouteData.Values["language"] == null)
{
HttpContext.Current.Request.RequestContext.RouteData.Values["language"] = "en-US";
}
上面的代码将URL变成以下形式,非常好: http://localhost:25576/en-US/Home 问题是,当用户输入http://localhost:25576/Home(没有en-US)时,
HttpContext.Current.Request.RequestContext.RouteData.Values["language"]
的值变成了"Home"。我的问题是,如果用户删除了en-US或输入http://localhost:25576/Home,如何获取语言参数的真实值?
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "DefaultLocalized",
url: "{language}/{controller}/{action}/{id}",
defaults: new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional,
language = ""
}
);
}
LocalizationAttribute
吗?此外,你的本地化路由应该在默认路由之后注册。 - msmolcic