Blazor WASM本地化日期格式与控制台应用不一致问题求助
Blazor WASM 日期本地化格式与控制台不一致的解决方法
问题说明
在控制台应用中,使用da-DK文化时,DateOnly.ToShortDateString()输出2023-07-25;但在Blazor WASM应用中,相同文化下DateTime.ToShortDateString()输出25.07.2023,期望两者格式保持一致。
控制台应用示例代码
CultureInfo ci = new("en-DK"); Thread.CurrentThread.CurrentCulture = ci; Thread.CurrentThread.CurrentUICulture = ci; var date = new DateOnly(2023, 07, 25); var rwerwer = date.ToShortDateString(); // da-DK Output: 2023-07-25 // en-DK Output: 25/07/2023
Blazor WASM 示例代码
Index.razor
@page "/" @using System.Globalization; @{ DateTime qweqw = new DateTime(2023, 07, 25); } <p>@qweqw.ToShortDateString()</p> @* da-DK Output: 25.07.2023 *@ @* en-DK Output: 25/07/2023 *@
Program.cs
var builder = WebAssemblyHostBuilder.CreateDefault(args); builder.RootComponents.Add<App>("#app"); builder.RootComponents.Add<HeadOutlet>("head::after"); builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); builder.Services.AddLocalization(); var culture = new CultureInfo("da_DK"); CultureInfo.DefaultThreadCurrentCulture = culture; CultureInfo.DefaultThreadCurrentUICulture = culture; await builder.Build().RunAsync();
.csproj
<PropertyGroup> <BlazorWebAssemblyLoadAllGlobalizationData>true</BlazorWebAssemblyLoadAllGlobalizationData> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.10" /> <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.10" PrivateAssets="all" /> <PackageReference Include="Microsoft.Extensions.Localization" Version="7.0.10" /> </ItemGroup>
问题原因
控制台应用使用.NET原生的全球化数据,da-DK的短日期格式为yyyy-MM-dd;而Blazor WASM默认依赖浏览器的Intl API进行本地化,浏览器对da-DK的短日期格式定义为dd.MM.yyyy,两者格式规则不一致导致输出差异。
解决方法
方法一:全局配置使用.NET原生全球化数据
修改Program.cs,配置本地化时强制使用.NET自带的全球化规则,确保格式与控制台对齐:
var builder = WebAssemblyHostBuilder.CreateDefault(args); builder.RootComponents.Add<App>("#app"); builder.RootComponents.Add<HeadOutlet>("head::after"); builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); // 配置本地化规则 builder.Services.AddLocalization(options => options.ResourcesPath = "Resources") .Configure<RequestLocalizationOptions>(options => { var supportedCultures = new[] { new CultureInfo("da-DK"), new CultureInfo("en-DK") }; options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("da-DK"); options.SupportedCultures = supportedCultures; options.SupportedUICultures = supportedCultures; }); // 设置线程文化 var culture = new CultureInfo("da-DK"); CultureInfo.DefaultThreadCurrentCulture = culture; CultureInfo.DefaultThreadCurrentUICulture = culture; await builder.Build().RunAsync();
确保.csproj中BlazorWebAssemblyLoadAllGlobalizationData已设为true,该配置会加载完整的.NET全球化数据,保证格式符合标准。
方法二:手动指定日期格式
如果不需要全局修改,可在格式化日期时直接使用.NET标准的da-DK格式字符串:
@page "/" @using System.Globalization; @{ DateTime qweqw = new DateTime(2023, 07, 25); var daCulture = new CultureInfo("da-DK"); } <!-- 使用.NET原生短日期格式 --> <p>@qweqw.ToString(daCulture.DateTimeFormat.ShortDatePattern)</p> <!-- 或直接指定格式字符串 --> <p>@qweqw.ToString("yyyy-MM-dd")</p>
两种方式都会输出2023-07-25,与控制台保持一致。
内容的提问来源于stack exchange,提问作者Niputi
相关产品推荐
相关产品推荐

