简体   繁体   English

如何在asp.net核心剃须刀页面2.2中设置日期格式和区域性

[英]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 : 这里有两个问题:

  1. How to properly localize a web appication and 如何正确定位网络应用程序以及
  2. How to use a custom culture ? 如何使用习俗文化?

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应用程序使用多个线程,如果您设置当前线程的CurrentCultureCurrentUICulture ,其他线程可能会有所不同。

Try use DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture : 尝试使用DefaultThreadCurrentCultureDefaultThreadCurrentUICulture

CultureInfo.DefaultThreadCurrentUICulture = culture;
CultureInfo.DefaultThreadCurrentCulture = culture;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Asp.Net Core 中为日期绑定设置文化? - How to set culture for date binding in Asp.Net Core? 使用 Razor Pages 在 Asp.NET Core 2.2 中找不到 ViewComponent - ViewComponent not found in Asp.NET Core 2.2 using Razor Pages 在razor,asp.net Core 2.2页面之间传递值 - Passage of values ​between pages razor, asp.net Core 2.2 如何将ASP.NET Core 2.2 Razor页面与MySql数据库连接? - How to connect ASP.NET Core 2.2 Razor pages with MySql database? 带ASP.NET Core 2.2的Razor页面中的嵌套/子区域 - Nested/Sub-Areas in Razor Pages w/ASP.NET Core 2.2 ASP.NET Core 2.2 Razor页面MVVM查询AspNetUsers和AspNetRoles表 - ASP.NET Core 2.2 Razor Pages MVVM Query the AspNetUsers and AspNetRoles Tables ASP.NET Core 2.2 Razor Pages 为什么 OnPost() 有效,而 OnPostAsync() 无效? - ASP.NET Core 2.2 Razor Pages why does OnPost() work, but OnPostAsync() doesn't? IIS 部署不能在 asp.net core 2.2 razor 页面中包含 wwwroot 文件夹的文件 - IIS deployment cannot contains files for wwwroot folder in asp.net core 2.2 razor pages 如何在 ASP.NET Core Razor 页面中使用 _ViewStart 设置条件布局? - How can I set up conditional Layouts with _ViewStart in ASP.NET Core Razor Pages? 如何为授权挑战设置两个不同的登录路径? (ASP.NET Core Razor 页面) - How do I set two different login paths for authorization challenge? (ASP.NET Core Razor Pages)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM