ASP.NET MVC中,本地化路线,为用户的默认语言 [英] ASP.NET mvc, localized routes and the default language for the user

查看:126
本文介绍了ASP.NET MVC中,本地化路线,为用户的默认语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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 is example.com/sv/Controller/Action.

但我怎么确保当用户进入该网站,他/她还是直接在正确的语言?我不知道如何得到我想要的语言,这不是问题。我以前做的事情是,我把文化融入到的RegisterRoutes 方法。但是因为我的网页是在集成模式下,我不能得到的Application_Start请求。

But how do I make sure that 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?

推荐答案

这是我会怎么做。

~~免责声明:伪code ~~

~~ 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
        );

}

记:控制器和/或动作不需要以第一和第二。其实,他们并不需要在所有的存在,在参数网​​址部分。

然后...

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 ..
}

奖金的建议

您还可以添加一个路由约束您的路线,因此它只接受了语言code 参数某些字符串。 <一href=\"http://blogs.microsoft.co.il/blogs/bursteg/archive/2009/01/11/asp-net-mvc-route-constraints.aspx\">So偷这家伙的code ....

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 ....

(更多pseduo code)...

(more pseduo 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆