我已经在.net核心应用中实现了本地化,并且我正在使用Cookie来改变文化。现在,我希望当前的文化也应该包含在网址中。如何实现此目标。
要更改网址,我正在做什么:-
$(document).ready(function(){
$("select[name='Current']").change(function(){
$("select[name='Aspired']").children("option").attr('disabled', false) // makes all options to selectable in case of new selection in Current select
for (i = 0; i < $(this).children("option:selected").val(); i++) {
$("select[name='Aspired']").children('option[value='+ i +']').attr('disabled', true) // disabling all the values uptil the select value
}
});
});
我正在从线程中获取当前区域性,并将其传递到我的路由,但是它始终在url中显示默认区域性。当我使用Cookie更改文化时,URL中的文化不变。
任何帮助将不胜感激。
编辑:-我的配置服务如下:-
'''
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseAuthentication();
var locOptions =
app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
var culture = cultureInfo.Name;
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
app.UseSession();
//configuring mvc routes...
app.UseMvc(routes =>
{
routes.MapRoute(
name: "areaRoute",
template: "{area:exists}/{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" });
routes.MapRoute(
name: "default",
template: "{culture}/{controller}/{action}/{id?}",
defaults: new { culture = culture, controller = "Home", action = "Index" });
});
}
我在应用程序中有语言更改选项。在我看来,我是这样做的:-
public void ConfigureServices(IServiceCollection services)
{
//add authentication before all services
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
{
options.AccessDeniedPath = "/Account/Forbidden";
options.LoginPath = "/Account/Login";
});
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromDays(30);//You can set Time
});
//adding HttpContextAccessor middleware
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddRouting(options => options.LowercaseUrls = true);
//configuration for multilanguage support
services.AddLocalization();
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
// services.AddMvc()
//.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
//.AddDataAnnotationsLocalization();
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("de"),
new CultureInfo("en")
};
// CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
options.DefaultRequestCulture = new RequestCulture(culture: "de", uiCulture: "de");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
//options.RequestCultureProviders.Insert(0, new RouteValueRequestCultureProvider(supportedCultures));
options.RequestCultureProviders = new List<IRequestCultureProvider>
{
new QueryStringRequestCultureProvider(),
new CookieRequestCultureProvider()
};
});
//adding other services...
ConfigureBusinessServices(services);
}
我的控制器要改变文化:-
@using Microsoft.AspNetCore.Builder
@using Microsoft.AspNetCore.Http.Features
@using Microsoft.AspNetCore.Localization
@using Microsoft.AspNetCore.Mvc.Localization
@using Microsoft.Extensions.Options
@inject IViewLocalizer Localizer
@inject IOptions<RequestLocalizationOptions> LocOptions
@{
var requestCulture = Context.Features.Get<IRequestCultureFeature>();
var cultureItems = LocOptions.Value.SupportedUICultures
.Select(c => new SelectListItem { Value = c.Name, Text = c.DisplayName })
.ToList();
var returnUrl = string.IsNullOrEmpty(Context.Request.Path) ? "~/" : $"~{Context.Request.Path.Value}";
}
<div title="@Localizer["Request culture provider:"] @requestCulture?.Provider?.GetType().Name">
<form id="selectLanguage" asp-controller="Language"
asp-action="ChangeLanguage" asp-route-returnUrl="@returnUrl"
method="post" class="form-horizontal" role="form">
<label asp-for="@requestCulture.RequestCulture.UICulture.Name">@Localizer["Language:"]</label> <select name="culture"
onchange="this.form.submit();"
asp-for="@requestCulture.RequestCulture.UICulture.Name" asp-items="cultureItems"></select>
</form>
</div>
问题是我的应用程序启动时,默认情况下也在url中显示德国文化。但是当我将语言更改为英语时。这种文化正在改变我用英语显示的所有数据,但网址仍然显示德语。