MVC3全球化:全球需要过滤模型之前绑定 [英] MVC3 globalization: need global filter before model binding

查看:164
本文介绍了MVC3全球化:全球需要过滤模型之前绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有一个名为全局过滤器 GlobalizationFilter 来检查路由值,cookies和浏览器语言的头,以确定请求的正确的文化设置:

Currently, I have a global filter called GlobalizationFilter that checks the route values, cookies and browser languages header to determine the correct culture settings for the request:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    // determine cultureInfo
    Thread.CurrentThread.CurrentCulture = cultureInfo;
    Thread.CurrentThread.CurrentUICulture = cultureInfo;
}

这所有的作品,但模型绑定过程似乎全局筛选器之前发生,所以模型绑定不走的文化设置生效。

It all works, but the model binding process seems to occur before the global filters, and so the model binder doesn't take the culture settings into account.

这导致与跨preting双重价值的问题,的DateTime 值等。

This leads to problems with interpreting double values, DateTime values etc.

我可以移动检测文化code到其它位置,但我不喜欢我的任何选项:

I could move the culture detection code to other locations, but I don't like any of my options:


  • 应用程序的的BeginRequest 事件。在这一点上并没有发生路由的时候,所以我不得不手动捞出URL中的 / EN-US / 文化象征。这是不可接受的。

  • Application's BeginRequest event. At this point of time the routing hasn't occurred, so I'll have to manually fish out the /en-US/ culture token from the URL. This in unacceptable.

控制器的初始化()方法。这将迫使我写我的所有控制器的基类,并从它继承了现有的控制器。我不喜欢这样,但我会选择这种解决方案,如果没有更好的出现。

Controller's Initialize() method. This will force me to write a base class for all my controllers, and inherit the existing controllers from it. I don't like this, but I'll opt for this solution if nothing better comes up.

在理想情况下,我想找到某种方式来注入的我code中的传送完成和模型绑定启动事件,但我发现没有什么MSDN /谷歌这一点。

Ideally, I want to find some way to inject my code between the "routing complete" and "model binding starts" events, but I found nothing in MSDN / Google on this.

或者,也许有一些其他的方式来处理MVC3全球化的,我是不知道的?

Or maybe there's some other way to handle MVC3 globalization that I'm unaware of?

在此先感谢任何贡献。

推荐答案

中提取出来的code决定了文化到一个单独的组件/类。然后创建一个 ModelBinder的 DefaultModelBinder 得出结论说使用类调用之前设置文化 BindModel

Extract out the code that determines the culture into a separate component/class. Then create a ModelBinder that derives from DefaultModelBinder that uses the class to set the culture before calling BindModel

public class CultureAwareModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        /* code that determines the culture */
        var cultureInfo = CultureHelper.GetCulture(controllerContext.HttpContext);

        //set current thread culture
        Thread.CurrentThread.CurrentCulture = cultureInfo;
        Thread.CurrentThread.CurrentUICulture = cultureInfo;

        return base.BindModel(controllerContext, bindingContext);
    }
}

和然后将其注册为应用程序(在的Application_Start

and then register it for the application (in Application_Start)

// register our own model binder as the default
ModelBinders.Binders.DefaultBinder = new CultureAwareModelBinder();

这篇关于MVC3全球化:全球需要过滤模型之前绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆