我从ASP.NET core samples复制了语言选择器,并将SetLanguage方法添加到我的
[HttpPost]
public IActionResult SetLanguage(string culture, string returnUrl)
{
Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
);
return LocalRedirect(returnUrl);
}
但是,当我更改语言选择时,永远不会调用控制器,也不会出现网络请求或JavaScript错误。关于缺少什么的任何建议。
@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();
}
<div title="@Localizer["Request culture provider:"] @requestCulture?.Provider?.GetType().Name">
<form id="selectLanguage" asp-controller="Home" asp-action="SetLanguage" asp-route-returnUrl="@Context.Request.Path" method="post" class="form-horizontal" role="form">
@Localizer["Language:"] <select name="culture" asp-for="@requestCulture.RequestCulture.UICulture.Name" asp-items="cultureItems"> </select>
</form>
</div>
呈现的HTML如下:
<div title="Request culture provider: AcceptLanguageHeaderRequestCultureProvider">
<form id="selectLanguage" method="post" class="form-horizontal" role="form" action="/Home/SetLanguage?returnUrl=%2F">
Language: <select name="culture" id="requestCulture_RequestCulture_UICulture_Name"><option selected="selected" value="en-GB">English (United Kingdom)</option>
<option value="en-US">English (United States)</option>
<option value="en">English</option>
<option value="de">Deutsch</option>
<option value="fr">français</option>
</select>
<input name="__RequestVerificationToken" type="hidden" value="CfDJ8GV_4Y4TrKRMt52xrAg49HFabVGQ6RTHVo2cPjiA2tk3yAtt7EFi-dr3P6wRdR6vj0FqDcXgKwvYef8HrVWa9tFupW9eTJMOBQ848jT_zG2t2-0_XNuAFmnOYfAu3DNBuS6DpF8RF8TfmRdTktOQYzk"></form>
</div>
答案 0 :(得分:0)
你是否添加了javascript以进行选择自动发布?
(function () {
$("#selectLanguage select").change(function () {
$(this).parent().submit();
});
}());