Url MVC web 项目中的本地化

Url Localization in MVC web Project

我有一个多语言页面,我将文化信息存储在 cookie 中。但现在我将其更改为 URL 本地化。 Url 应该看起来像

www.domain.com/en/home/index 或 www.domain.com/fr/home/index

我尝试了很多解决方案,但没有一个奏效。现在我有一个解决方案,但它只适用于路线而不适用于区域。

在Global.asax中我注册了像

这样的路由
   protected void Application_Start()
    {
     //   ViewEngines.Engines.Clear();

        //LocalizedViewEngine: this is to support views also when named e.g. Index.de.cshtml
        ViewEngines.Engines.Insert(0, new LocalizedViewEngine());

        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);



        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        //standard mvc4 routing. see App_Start\RouteConfig.cs
        //RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();
    }



        public static void RegisterRoutes(RouteCollection routes)
    {
        const string defautlRouteUrl = "{controller}/{action}/{id}";

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        RouteValueDictionary defaultRouteValueDictionary = new RouteValueDictionary(new { controller = "Home", action = "Index", id = UrlParameter.Optional });
        routes.Add("DefaultGlobalised", new GlobalisedRoute(defautlRouteUrl, defaultRouteValueDictionary));
        routes.Add("Default", new Route(defautlRouteUrl, defaultRouteValueDictionary, new MvcRouteHandler()));

        //LocalizedViewEngine: this is to support views also when named e.g. Index.de.cshtml
        routes.MapRoute(
            "Default2",
            "{culture}/{controller}/{action}/{id}",
            new
            {
                culture = string.Empty,
                controller = "Home",//ControllerName
                action = "Index",//ActionName
                id = UrlParameter.Optional
            }
        ).RouteHandler = new LocalizedMvcRouteHandler();
    }

并且在每个 AreaRegistration 中我都有这个覆盖

 public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Doc_default",
            "Doc/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );

        //LocalizedViewEngine: this is to support views also when named e.g. Index.de.cshtml
        context.MapRoute(
            "Doc_default2",
            "{culture}/Doc/{controller}/{action}/{id}",
            new
            {
                culture = string.Empty,
                controller = "Doc",
                action = "Index",
                id = UrlParameter.Optional
            }
        ).RouteHandler = new LocalizedMvcRouteHandler();
    }

我花了几个小时解决这个问题,但我不明白!有没有好的MVC教程URL本地化?

感谢您的帮助!

所以现在我有了适合自己的解决方案。而且效果很好。

我现在在registerRroutes和RegisterArea

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
                name: "Default_Localization",
                url: "{language}/{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, language = string.Empty }
                );
    }

然后是 BaseController

  protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
    {

        string cultureName;
        if (RouteData.Values.ContainsKey("language") && !string.IsNullOrWhiteSpace(RouteData.Values["language"].ToString()))
        {
            cultureName = RouteData.Values["language"].ToString();
        }
        else
        {
            cultureName = Request.UserLanguages != null && Request.UserLanguages.Length > 0 ? CultureHelper.GetNeutralCulture(Request.UserLanguages[0]) : null;
            cultureName = CultureHelper.GetImplementedCulture(cultureName);
        }

        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureName.ToLower());
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName.ToLower());

        return base.BeginExecuteCore(callback, state);
    }

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);

        var cultureName = CultureHelper.GetNeutralCulture(CultureHelper.GetCurrentCulture());
        cultureName = CultureHelper.GetImplementedCulture(cultureName);
        filterContext.RouteData.Values["language"] = cultureName.ToUpper();
    }

就是这样!