ASP.NET MVC(异步)CurrentCulture 未在 Controller 和 View 之间共享
ASP.NET MVC (Async) CurrentCulture is not shared between Controller and View
我有一个针对 .NET Framework 4.7.1 的 ASP.NET MVC 4 应用程序,问题是如果操作包含异步调用,控制器和视图之间无法共享文化。
我正在引用 NuGet 包 Microsoft.AspNet.Mvc
5.2.3(并且可以在 5.2.4 中复制)。
这是控制器中的代码:
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();
}
}
这是视图中的代码:
@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.
,我找不到与此问题相关的任何 post
Am I not correctly handling the CurrentCulture, or this is how it is supposed to work?
你不是。当前 UI 线程的文化需要在 进入异步操作方法之前 进行设置。如果您在异步方法中设置文化,它对 UI 线程没有影响(如您所见)。
但是抛开异步问题不谈,在 application lifecycle of MVC 中设置动作方法内部的文化 太迟了,因为一些重要的 culture-sensitive在那之前 MVC 的特性(即模型绑定)运行。
在生命周期中尽早设置区域性的一种方法是使用授权过滤器,这可确保在模型绑定发生之前设置区域性。
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
添加为类似于 的路由值,但如果需要,您可以设计一个过滤器以从其他地方获取文化.
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.
我有一个针对 .NET Framework 4.7.1 的 ASP.NET MVC 4 应用程序,问题是如果操作包含异步调用,控制器和视图之间无法共享文化。
我正在引用 NuGet 包 Microsoft.AspNet.Mvc
5.2.3(并且可以在 5.2.4 中复制)。
这是控制器中的代码:
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();
}
}
这是视图中的代码:
@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 中设置动作方法内部的文化 太迟了,因为一些重要的 culture-sensitive在那之前 MVC 的特性(即模型绑定)运行。
在生命周期中尽早设置区域性的一种方法是使用授权过滤器,这可确保在模型绑定发生之前设置区域性。
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
添加为类似于
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.