c# - ASP.NET MVC(异步)CurrentCulture 不在 Controller 和 View 之间共享

标签 c# asp.net-mvc async-await cultureinfo currentuiculture

我有一个以 .NET Framework 4.7.1 为目标的 ASP.NET MVC 4 应用程序,如果操作包含异步调用,则存在文化在 Controller 和 View 之间不共享的问题。

我正在引用 NuGet 包 Microsoft.AspNet.Mvc 5.2.3(并且可以在 5.2.4 中复制)。

这是 Controller 中的代码:

public class CulturesTestController : Controller
{
    public async Task<ActionResult> Index(string value)
    {
        Thread.CurrentThread.CurrentCulture = 
            CultureInfo.GetCultureInfo("fi-FI");
        Thread.CurrentThread.CurrentUICulture = 
            CultureInfo.GetCultureInfo("fi-FI");
        var model = new CulturesContainer
        {
            CurrentCulture = Thread.CurrentThread.CurrentCulture,
            CurrentUICulture = Thread.CurrentThread.CurrentUICulture,
            CurrentThreadId = Thread.CurrentThread.ManagedThreadId
        };
        Log.Write(Level.Info, "CurrentUICulture - Before Await - " +
                              "CurrentCulture: " +
                              $"{Thread.CurrentThread.CurrentCulture}, " +
                              "CurrentUICulture: "
                              ${Thread.CurrentThread.CurrentUICulture} -> "+
                              "ThreadId: " + 
                              $"{Thread.CurrentThread.ManagedThreadId}");

        await GetAwait();
        Log.Write(Level.Info, "CurrentUICulture - After Await - " +
                              "CurrentCulture: " + 
                              $"{Thread.CurrentThread.CurrentCulture}, " +
                              "CurrentUICulture: " +
                              $"{Thread.CurrentThread.CurrentUICulture} -> " +
                              "ThreadId: " +
                              $"{Thread.CurrentThread.ManagedThreadId}");
        return View("Index", model);
    }

    public class CulturesContainer
    {
        public CultureInfo CurrentCulture { get; set; }
        public int CurrentThreadId { get; set; }
        public CultureInfo CurrentUICulture { get; set; }
    }

    private async Task GetAwait()
    {
        await Task.Yield();
    }
}

这是 View 中的代码:

@using System.Globalization
@using System.Threading
@model CultureTest.Controllers.CulturesTestController.CulturesContainer
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <title>title</title>
</head>
<body>
    <div>
        InitialCurrentCulture = {
        <label>@Html.Raw(Model.CurrentCulture)</label>
        }  --
        InitialCurrentUICulture = {
        <label>@Html.Raw(Model.CurrentUICulture)</label>
        }  --
        InitialThreadId = {
        <label>@Html.Raw(Model.CurrentThreadId)</label>
        }
        <br />
        ActualCurrentCulture = {
        <label>@Html.Raw(CultureInfo.CurrentCulture)</label>
        }  --
        ActualCurrentUICulture = {
        <label>@Html.Raw(CultureInfo.CurrentUICulture)</label>
        }  --
        ActualThreadId = {
        <label>@Html.Raw(Thread.CurrentThread.ManagedThreadId)</label>
        }
    </div>
</body>
</html>

日志如下:

20180320-12:04:25.357-12   -INFO -CulturesTestController+<Index>d__0: 
    CurrentUICulture - Before Await - 
    CurrentCulture: fi-FI, CurrentUICulture: fi-FI -> ThreadId: 12
20180320-12:04:25.357-8    -INFO -CulturesTestController+<Index>d__0: 
    CurrentUICulture - After Await - 
    CurrentCulture: fi-FI, CurrentUICulture: fi-FI -> ThreadId: 8

网页显示时:

InitialCurrentCulture = { fi-FI } -- 
InitialCurrentUICulture = { fi-FI } -- InitialThreadId = { 12 } 
ActualCurrentCulture = { en-US } --
ActualCurrentUICulture = { en-US } -- ActualThreadId = { 9 } 

.NET Framework < 4.6 中有一个问题已在 4.6 中修复,但似乎该问题在 MVC 中仍然存在。

If the thread is a thread pool thread that is executing a task-based asynchronous operation and the app targets the .NET Framework 4.6 or a later version of the .NET Framework, its UI culture is determined by the UI culture of the calling thread. (Source https://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentuiculture(v=vs.110).aspx)

我是否没有正确处理 CurrentCulture,或者它应该如何工作?对于 .NET Framework 4.x,我找不到与此问题相关的任何帖子。

最佳答案

Am I not correctly handling the CurrentCulture, or this is how it is supposed to work?

你不是。当前 UI 线程的文化需要在进入您的异步操作方法之前进行设置。如果您在异步方法中设置区域性,它对 UI 线程没有影响(如您所见)。

但抛开异步问题不谈,在 application lifecycle of MVC为时已晚在操作方法中设置文化,因为 MVC 的一些重要的文化敏感功能(即模型绑定(bind))在此之前运行。

在生命周期中尽早设置文化的一种方法是使用授权过滤器,这可确保在模型绑定(bind)发生之前设置文化。

using System.Globalization;
using System.Threading;
using System.Web.Mvc;

public class CultureFilter : IAuthorizationFilter
{
    private readonly string defaultCulture;

    public CultureFilter(string defaultCulture)
    {
        this.defaultCulture = defaultCulture;
    }

    public void OnAuthorization(AuthorizationContext filterContext)
    {
        var values = filterContext.RouteData.Values;

        string culture = (string)values["culture"] ?? this.defaultCulture;

        CultureInfo ci = new CultureInfo(culture);

        Thread.CurrentThread.CurrentCulture = ci;
        Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(ci.Name);
    }
}

并在全局注册:

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new CultureFilter(defaultCulture: "fi-FI"));
        filters.Add(new HandleErrorAttribute());
    }
}

以上示例假定您已设置路由以将 culture 添加为类似于 this answer 的路由值,但如果需要,您可以设计一个过滤器以从其他地方获取文化。

NOTE: I realize this question is not about .NET Core, but as @GSerg pointed out in the comments, this same issue was reported as a bug for ASP.NET Core, and it was closed as by design. The conclusion they came to was the same:

Setting the culture inside a resource filter would apply to both the action and view.

关于c# - ASP.NET MVC(异步)CurrentCulture 不在 Controller 和 View 之间共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49382172/

相关文章:

javascript - 如何在恢复功能之前等待 JavaScript Promise 解决?

c# - 使用 ChannelFactory 和 CreateChannel 进行异步 WCF 调用

c# - 命令提示符未打开但在.Net中执行命令

c# - 调试 AxShockwaveFlash

asp.net-mvc - v下一步。 AspNet.Identity 和自定义 UserStore。 UserStore 处理异常

javascript - Bootstrap Modal 传递 ID 不起作用

c# - 多次异步请求相同的 Web 服务

c# - 绑定(bind)到使用 CreateObject 创建的已打开表单

asp.net-mvc - MVCContrib 支持 MVC4 吗?

c# - 如何处理 xUnit .net 的 Assert.Throws<T> 中任务抛出的异常?