ASP.NET 5.0 - 如何启动并运行 CookieRequestCultureProvider
Posted
技术标签:
【中文标题】ASP.NET 5.0 - 如何启动并运行 CookieRequestCultureProvider【英文标题】:ASP.NET 5.0 - How to get CookieRequestCultureProvider up and running 【发布时间】:2021-09-27 18:37:08 【问题描述】:本地化 - 设置
我无法使用 cookie 使本地化工作。我很高兴了解如何最终整合这项技术。不得不说,我是开发者世界的新手,为了自己解决这个问题,我尝试研究了互联网上的所有方法。
到目前为止,Startup.cs 中的默认设置对我来说是可以理解的。在其他课程中,我必须重新理解。我目前不知道如何以及在何处使用 CookieRequestCultureProvider 类来使网站的 cookie 功能正常工作。
提前致谢。
PS:我参与了以下教程。https://www.mikesdotnetting.com/article/345/localisation-in-asp-net-core-razor-pages-cultureshttps://www.mikesdotnetting.com/article/346/using-resource-files-in-razor-pages-localisation
以下是我目前的设置:
dotnet --info
.NET SDK (reflecting any global.json):
Version: 5.0.301
Commit: ef17233f86
Runtime Environment:
OS Name: Windows
OS Version: 10.0.19043
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\5.0.301\
Host (useful for support):
Version: 5.0.7
Commit: 556582d964
.NET SDKs installed:
5.0.301 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.16 [C:\ProgramFiles\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
startup.cs | ConfigureServices
public void ConfigureServices(IServiceCollection services)
// Localization - adding resource path
services.AddLocalization(options =>
// We will put our translations in a folder called Resources
options.ResourcesPath = "Resources";
);
// Localization - look up MS docs
// Add framework services.
services.AddMvc()
// adding localization for views
.AddViewLocalization()
// e.g. [Display(Name = "Remember Me?")]
.AddDataAnnotationsLocalization(options =>
options.DataAnnotationLocalizerProvider = (type, factory) =>
var assemblyName = new AssemblyName(typeof(CommonResources).GetTypeInfo().Assembly.FullName);
return factory.Create(nameof(CommonResources), assemblyName.Name);
;
);
// we are configuring the localization service to support a list of provided cultures.
services.Configure<RequestLocalizationOptions>(options =>
// Localization - the list of supported cultures
// combination of parent language and region
var supportedCultures = new List<CultureInfo>
new CultureInfo("de-DE"),
new CultureInfo("en-GB"),
new CultureInfo("nl-NL")
;
// State what the default culture for your application is. This will be used if no
// specific culture can be determined for a given request.
options.DefaultRequestCulture = new RequestCulture(
culture: supportedCultures[0],
uiCulture: supportedCultures[0]);
// You must explicitly state which cultures your application supports.
// These are the cultures the app supports for formatting numbers, dates, etc.
options.SupportedCultures = supportedCultures;
// UI strings that we have localized
options.SupportedUICultures = supportedCultures;
);
...
startup.cs | Configure
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider services)
...
app.UseRouting();
// Localization - middleware
// 1.QueryStringRequestCultureProvider – culture and ui - culture are passed in the query string.
// 2.CookieRequestCultureProvider – culture is set in the ASP.NET Core culture cookie.
// 3.AcceptLanguageHeaderRequestCultureProvider – which gets the culture from the browser’s language set by the user.
// specify that globalization is being used in the pipeline.
var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
...
app.UseEndpoints(endpoints =>
endpoints.MapRazorPages();
);
Culture Switcher Razor Component
<div>
<form id="culture-switcher">
<select name="culture" id="culture-options">
<option></option>
@foreach (var culture in Model.SupportedCultures)
<option value="@culture.Name" selected="@(Model.CurrentUICulture.Name == culture.Name)">@culture.ThreeLetterWindowsLanguageName</option>
</select>
</form>
</div>
<script>
document.getElementById("culture-options").addEventListener("change", () =>
document.getElementById("culture-switcher").submit();
);
</script>
CommonLocalizationService.cs
using KarriereWebseiteRazor.Resources;
using Microsoft.Extensions.Localization;
using System.Reflection;
namespace KarriereWebseiteRazor.Services
public class CommonLocalizationService
private readonly IStringLocalizer _localizer;
public CommonLocalizationService(IStringLocalizerFactory factory)
var assemblyName = new AssemblyName(typeof(CommonResources).GetTypeInfo().Assembly.FullName);
_localizer = factory.Create(nameof(CommonResources), assemblyName.Name);
public string Get(string key)
return _localizer[key];
【问题讨论】:
【参考方案1】:也许实现你想要的最简单的方法是更改 ViewComponent 中的 javascript 以设置 cookie:
const cultureOptions = document.getElementById("culture-options");
cultureOptions.addEventListener("change", () =>
const culture = cultureOptions.value;
document.cookie = `.AspNetCore.Culture=c=$culture|uic=$culture`;
location.reload()
);
【讨论】:
谢谢迈克。像魅力一样工作。以上是关于ASP.NET 5.0 - 如何启动并运行 CookieRequestCultureProvider的主要内容,如果未能解决你的问题,请参考以下文章
IIS 5.0 和 6.0 的 ASP.NET 应用程序生命周期概述
如何强制 Quartz 现在在 ASP.NET Core 中启动?
无法在 ASP.NET Core 5.0 下的 IIS 中托管 CoreWcf
Entity Framework Core 5.0 如何将多对多连接的 LINQ 转换为使用 ASP.NET 成员资格的交集表
为 ASP.NET Core 5.0 - EF Core 5.0 Web App 配置 PostgreSQL 连接字符串以在 MS 或 Linux 云上运行?