[英]How to set date formats and culture in asp.net core razor pages 2.2
I'm using Asp.Net Core 2.2 Razor Pages application. 我正在使用Asp.Net Core 2.2 Razor Pages应用程序。 I want to set application culture and date formats in application level. 我想在应用程序级别设置应用程序区域性和日期格式。 So after some googling I end up adding the below code in ConfigureServices
method in Startup.cs 因此,经过一番谷歌搜索后,我最终在Startup.cs的 ConfigureServices
方法中添加了以下代码
public void ConfigureServices(IServiceCollection services)
{
// setting the application culture to en-US with MM/dd/yyyy short date pattern.
var culture = CultureInfo.CreateSpecificCulture("en-US");
var dateformat = new DateTimeFormatInfo { ShortDatePattern = "MM/dd/yyyy", LongDatePattern = "MM/dd/yyyy hh:mm:ss tt" };
culture.DateTimeFormat = dateformat;
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
But this doesn't seem to work. 但这似乎不起作用。 I'm getting the date in wrong format. 我的日期格式错误。 Example, DateTime.Now.ToShortDateString()
returns 6/25/2019 instead of 06/25/2019 . 例如, DateTime.Now.ToShortDateString()
返回6/25/2019而不是06/25/2019 。 The same is the problem with DateTime.Now.ToLongDateString()
DateTime.Now.ToLongDateString()
也是同样的问题
And If I change the format to "dd/MM/yyyy" will it return the date in 25/06/2019 ?? 如果我将格式更改为“ dd / MM / yyyy”,它将返回日期25/06/2019 ?
I'm running this in my local development environment. 我正在本地开发环境中运行它。 Also what I'm expecting is that my app to get the dates in the same format I set above in any environment, any machine, any culture. 我还期望我的应用程序可以在任何环境,任何计算机,任何区域性下以与我上面设置的格式相同的格式获取日期。
Please assist and guide me on where I'm wrong. 请协助并指导我哪里做错了。
Thanks in advance!! 提前致谢!!
There are two questions here : 这里有两个问题:
ASP.NET Core and ASP.NET offer their own globalization and localization features that work on multiple levels (application, page, request). ASP.NET Core和ASP.NET提供了它们自己的全球化和本地化功能,它们可以在多个级别(应用程序,页面,请求)工作。 You should check Globalization and Localization in ASP.NET Core to understand how these features work in ASP.NET Core. 您应该检查ASP.NET Core中的全球化和本地化,以了解这些功能如何在ASP.NET Core中工作。 As everything else, the services are provided through middleware. 与其他所有内容一样,服务是通过中间件提供的。
There are a lot of blog posts about this too. 也有很多关于此的博客文章。 I picked this one due to the title Internationalization - ASP.NET Core: From 0 to overkill . 我之所以选择这一名称,是因为标题为Internationalization-ASP.NET Core:从0到overkill 。 It explains the same things the docs do, in a more concise way. 它以更简洁的方式说明了文档所做的相同操作。
For your specific question though, all that's needed is to set the culture for each request. 但是对于您的特定问题,所需要做的只是为每个请求设置区域性。 This can be done with the localization middleware's UseRequestLocalization method. 这可以通过本地化中间件的UseRequestLocalization方法来完成。
In Startup.cs's Configure(IApplicationBuilder, IWebHostEnvironment)
, right before app.UseStaticFiles()
you can add a call to UseRequestLocalization
to specify the cultures : 在Startup.cs的Configure(IApplicationBuilder, IWebHostEnvironment)
,就在app.UseStaticFiles()
之前,您可以添加对UseRequestLocalization
的调用以指定区域性:
var supportedCultures = new[]
{
new CultureInfo("ru-RU"),
};
app.UseRequestLocalization(new RequestLocalizationOptions{
DefaultRequestCulture = new RequestCulture("ru-RU"),
SupportedCultures=supportedCultures,
SupportedUICultures=supportedCultures
});
I picked ru-RU
because it uses a dot for a date separator. 我选择ru-RU
是因为它使用点作为日期分隔符。 Putting 把
@DateTime.Today.ToString()
in a Razor page this produces 在剃刀页面中产生
25.06.2019 0:00:00
You can pass your own custom culture to UseRequestLocalization
too. 您也可以将自己的自定义文化传递给UseRequestLocalization
。 Using this custom culture : 使用这种自定义文化:
var culture = CultureInfo.CreateSpecificCulture("en-US");
var dateformat = new DateTimeFormatInfo {
ShortDatePattern = "MM/dd/yyyy",
LongDatePattern = "MM/dd/yyyy hh:mm:ss tt"
};
culture.DateTimeFormat = dateformat;
var supportedCultures = new[]
{
culture
};
app.UseRequestLocalization(new RequestLocalizationOptions{
DefaultRequestCulture = new RequestCulture(culture),
SupportedCultures=supportedCultures,
SupportedUICultures=supportedCultures
});
Produces : 产生:
06/25/2019 00:00:00
Your WebApplication
use multiple threads, if you set a CurrentCulture
or CurrentUICulture
of your current thread, others may differ. 您的WebApplication
应用程序使用多个线程,如果您设置当前线程的CurrentCulture
或CurrentUICulture
,其他线程可能会有所不同。
Try use DefaultThreadCurrentCulture
and DefaultThreadCurrentUICulture
: 尝试使用DefaultThreadCurrentCulture
和DefaultThreadCurrentUICulture
:
CultureInfo.DefaultThreadCurrentUICulture = culture;
CultureInfo.DefaultThreadCurrentCulture = culture;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.