1
我有一個多語言頁面,我將文化信息存儲在cookie中。但現在我會將其更改爲URL本地化。網址看起來應該像MVC web項目的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本地化教程?
感謝您的幫助!
你可以使用屬性路由 - [見這裏的文章](https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/) –
[ASP.NET MVC 5在路由和URL中的文化]的可能重複(http://stackoverflow.com/questions/32764989/asp-net-mvc-5-culture-in-route-and-url) – NightOwl888