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!
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/) –
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