2016-05-12 16 views
1

Ich habe eine mehrsprachige Seite, wo ich die Kulturinformationen in einem Cookie speichern. Aber jetzt werde ich es in URL-Lokalisierung ändern. URL sollte folgendermaßen aussehenURL-Lokalisierung in MVC-Web-Projekt

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

Ich habe versucht, eine Menge Lösung, aber nichts funktionierte gut. Jetzt habe ich eine Lösung, aber es funktioniert nur auf der Route, aber nicht mit Bereichen.

in Global.asax registrieren ich die Routen wie

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(); 
    } 

und in jeder AreaRegistration ich diese

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(); 
    } 

ich für dieses Problem verbrachte Stunden überschreiben, aber ich verstehe es nicht! Gibt es ein gutes Tutorial für die Lokalisierung von MVC-URLs?

Danke für Hilfe!

+0

Sie könnten Attribut-Routing verwenden - [Siehe Artikel hier] (https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/) –

+0

Mögliches Duplikat von [ASP.NET MVC 5 Kultur in Route und URL] (http://stackoverflow.com/questions/32764989/asp-net-mvc-5-culture-in-route-and-url) – NightOwl888

Antwort

0

So jetzt habe ich die Lösung für mich. Und es funktioniert großartig.

Ich habe jetzt in registerRroutes und 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 } 
       ); 
    } 

Und dann die Base

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(); 
    } 

Das ist es!