问题描述
我正在使用Asp.Net Core 2.2 Razor Pages应用程序。我想在应用程序级别设置应用程序区域性和日期格式。因此,经过一番谷歌搜索之后,我最终在 Startup.cs
I'm using Asp.Net Core 2.2 Razor Pages application. 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
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;
但这似乎行不通。我的日期格式错误。例如,
DateTime.Now.ToShortDateString()
返回 2019/6/25 而不是 06/25/2019 。 DateTime.Now.ToLongDateString()
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. The same is the problem with DateTime.Now.ToLongDateString()
也是同样的问题,如果我将格式更改为 dd / MM / yyyy,它将返回 2019年6月25日中的日期?
And If I change the format to "dd/MM/yyyy" will it return the date in 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.
在此先感谢!!
推荐答案
此处有两个问题:
- 如何正确定位Web应用和
- 如何使用自定义文化?
ASP.NET Core和ASP.NET提供了它们自己的全球化和本地化功能,它们可以在多个级别(应用程序,页面,请求)工作。您应该检查了解这些功能如何在ASP.NET Core中工作。
ASP.NET Core and ASP.NET offer their own globalization and localization features that work on multiple levels (application, page, request). You should check Globalization and Localization in ASP.NET Core to understand how these features work in 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. 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.
在Startup.cs的 Configure(IApplicationBuilder,IWebHostEnvironment)
中,就在 app.UseStaticFiles之前()
,您可以向 UseRequestLocalization
添加呼叫以指定区域性:
In Startup.cs's Configure(IApplicationBuilder, IWebHostEnvironment)
, right before app.UseStaticFiles()
you can add a call to UseRequestLocalization
to specify the cultures :
var supportedCultures = new[]
{
new CultureInfo("ru-RU"),
};
app.UseRequestLocalization(new RequestLocalizationOptions{
DefaultRequestCulture = new RequestCulture("ru-RU"),
SupportedCultures=supportedCultures,
SupportedUICultures=supportedCultures
});
我选择了 ru-RU
,因为它使用了日期分隔符的点。放入
I picked ru-RU
because it uses a dot for a date separator. Putting
@DateTime.Today.ToString()
在Razor页面中产生
in a Razor page this produces
25.06.2019 0:00:00
您可以将自己的自定义文化传递给 UseRequestLocalization
也是。使用这种自定义文化:
You can pass your own custom culture to UseRequestLocalization
too. 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
});
产品:
06/25/2019 00:00:00
这篇关于如何在asp.net核心剃刀页面2.2中设置日期格式和区域性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!