1

我有多语言网站,每种语言都使用自己的文化信息。我尝试使用 ActionFilterAttribute 设置 culterinfo,但它在模型绑定后被触发。我也尝试了全局 asax BeginRequest,但 RouteData 尚不可用。

那么应该在哪里将 CurrentUICulture 设置为 modelbinder 也会使用所需的cultureinfo?

最大限度

4

1 回答 1

2

You can find what you can/need to override from this great article: An Introduction to ASP.NET MVC Extensibility

You could write a custom MvcRouteHandler:

public class LangRouteHandler : MvcRouteHandler
{
    protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        if (requestContext.RouteData.Values.ContainsKey("lang"))
        {
            var culture = new CultureInfo(requestContext.RouteData.Values["lang"].ToString());
            if (culture != null)
            {
                Thread.CurrentThread.CurrentUICulture = culture;
                Thread.CurrentThread.CurrentCulture = culture;
            }
        }

        return base.GetHttpHandler(requestContext);
    }
}

And use this routehandler in your route like this:

routes.MapRoute(
   name: "Default",
   url: "{controller}/{lang}/{action}/{id}",
   defaults: new { controller = "Home", lang = "en", action = "Index", id = UrlParameter.Optional }
).RouteHandler = new LangRouteHandler();
于 2012-10-25T11:21:59.370 回答