如何设置ASP.NET 5应用程序的语言环境? [英] How do I set the locale for an ASP.NET 5 application?
问题描述
我正在尝试对ASP.NET 5 Web应用程序的主机语言环境进行覆盖.大多数解决方案都引用<globalization/>
web.config元素,但这是特定于IIS的,似乎不适合新的ASP.NET模型.
I’m trying to do a blanket override of the host locale for an ASP.NET 5 web application. Most solutions refer to the <globalization/>
web.config element, but this is IIS-specific and doesn't seem to fit the new ASP.NET model.
我尝试过:
app.Use(next => context => {
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-AU");
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-AU");
return next(context);
});
这将被执行,但是这似乎对请求没有任何影响(也许是由于管道中广泛的Task
?)是否有更好的方法来完成此操作?
This gets executed, but this doesn’t seem to have any effect on the request (maybe due to the extensive Task
ing in the pipeline?) Is there a better way to accomplish this?
推荐答案
问题出在异步控制器上.您应该将默认区域性设置为所有线程:
The issue is with the async controller. You should set the default culture to all threads instead:
app.Use(next => context => {
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.GetCultureInfo("en-AU");
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo("en-AU");
return next(context);
});
您实际上可以将这些行放在Configure方法的顶部:
You can actually just put these lines at the top of the Configure method:
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.GetCultureInfo("en-AU");
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo("en-AU");
这篇关于如何设置ASP.NET 5应用程序的语言环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!