43

对于多语言 ASP.NET MVC 3 Web 应用程序,我在控制器工厂上确定Thread.CurrentThread.CurrentCultureandThread.CurrentThread.CurrentUICulture如下:

public class MyControllerFactory : DefaultControllerFactory {

    protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType) {

        //Get the {language} parameter in the RouteData
        string UILanguage;
        if (requestContext.RouteData.Values["language"] == null)
            UILanguage = "tr";
        else
            UILanguage = requestContext.RouteData.Values["language"].ToString();

        //Get the culture info of the language code
        CultureInfo culture = CultureInfo.CreateSpecificCulture(UILanguage);
        Thread.CurrentThread.CurrentCulture = culture;
        Thread.CurrentThread.CurrentUICulture = culture;

        return base.GetControllerInstance(requestContext, controllerType);
    }

}

上面的代码现在已经快一年了!所以,我愿意接受建议。

我在 Global.asax 文件上注册了这个,比如:

ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());

这很好用,但我不确定这是否是执行此类操作的最佳实践和最佳场所。

我还没有深入研究 的主要角色,ControllerFactory我无法将其与ActionFilterAttribute.

您认为进行此类操作的最佳地点是什么?

4

5 回答 5

57

ActionFilter我为此使用了全局变量,但最近我意识到,OnActionExecuting在某些情况下,在方法中设置当前文化为时已晚。例如,当 POST 请求后的模型到达控制器时,ASP.NET MVC 会为模型创建元数据。它发生在执行任何操作之前。因此,DisplayName此时使用默认区域性处理属性值和其他数据注释内容。

最终,我将当前文化设置为自定义IControllerActivator实现,它就像一个魅力。我想从请求生命周期的角度来看,在自定义控制器工厂中托管这个逻辑几乎是一样的,就像你今天一样。它比使用 global 更可靠ActionFilter

CultureAwareControllerActivator.cs

public class CultureAwareControllerActivator: IControllerActivator
{
    public IController Create(RequestContext requestContext, Type controllerType)
    {
        //Get the {language} parameter in the RouteData
        string language = requestContext.RouteData.Values["language"] == null ?
            "tr" : requestContext.RouteData.Values["language"].ToString();

        //Get the culture info of the language code
        CultureInfo culture = CultureInfo.GetCultureInfo(language);
        Thread.CurrentThread.CurrentCulture = culture;
        Thread.CurrentThread.CurrentUICulture = culture;

        return DependencyResolver.Current.GetService(controllerType) as IController;
    }
}

全球.asax.cs

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        ...
        ControllerBuilder.Current.SetControllerFactory(new DefaultControllerFactory(new CultureAwareControllerActivator()));
    }
}
于 2012-06-16T15:35:19.767 回答
7

我知道已经选择了一个分析器。我们使用的选项是在应用程序的 OnBeginRequest 事件中初始化线程当前区域性。这样可以确保在每个请求中都能发现文化

public void OnBeginRequest(object sender, EventArgs e)
{
   var culture = YourMethodForDiscoveringCulutreUsingCookie();
   System.Threading.Thread.CurrentThread.CurrentCulture = culture;
   System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
}
于 2014-11-20T16:40:06.553 回答
4

放置它的另一个地方是将该代码放在自定义 ActionFilter 的 OnActionExecuting 方法中,该方法可以在 GlobalFilters 集合中注册:

http://weblogs.asp.net/gunnarpeipman/archive/2010/08/15/asp-net-mvc-3-global-action-filters.aspx

于 2011-11-22T12:19:13.543 回答
3

而不是覆盖OnActionExecuting,你可以Initialize像这样覆盖这里

protected override void Initialize(RequestContext requestContext)
{
        string culture = null;
        var request = requestContext.HttpContext.Request;
        string cultureName = null;

        // Attempt to read the culture cookie from Request
        HttpCookie cultureCookie = request.Cookies["_culture"];
        if (cultureCookie != null)
            cultureName = cultureCookie.Value;
        else
            cultureName = request.UserLanguages[0]; // obtain it from HTTP header AcceptLanguages

        // Validate culture name
        cultureName = CultureHelper.GetValidCulture(cultureName); // This is safe

        if (request.QueryString.AllKeys.Contains("culture"))
        {
            culture = request.QueryString["culture"];
        }
        else
        {
            culture = cultureName;
        }

        Uitlity.CurrentUICulture = culture;

        base.Initialize(requestContext);
    }
于 2013-08-06T05:13:29.717 回答
0

如果您不使用 ControllerActivator,可以使用 BaseController 类并从中继承。

public class BaseController : Controller
{
    public BaseController()
    {
          //Set CurrentCulture and CurrentUICulture of the thread
    }
}

public class HomeController: BaseController    
{
    [HttpGet]
    public ActionResult Index()
    {
        //..............................................
    }
}
于 2019-04-17T14:17:49.203 回答