我有一个多语言页面,我将文化信息存储在cookie中。但现在我将其更改为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本地化教程?
感谢您的帮助!
答案 0 :(得分:0)
所以现在我有了解决方案。而且效果很好。
我现在在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();
}
多数民众赞成!