[英]Multilingual Asp.Net Core 3.1 MVC application does not showing language code in url
I'm working on an ASP.NET CORE application with multi lingual content.我正在开发具有多语言内容的 ASP.NET CORE 应用程序。 The culture is getting change successfully and it can be viewed in application cookies as well.
文化正在成功改变,它也可以在应用程序 cookie 中查看。
But the URL not changing by language switch.但 URL 不会因语言切换而改变。 What I have tried in startup is as follow:
我在启动时尝试过的内容如下:
Startup ConfigureServices:启动配置服务:
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix,
options => { options.ResourcesPath = "Resources"; })
.AddDataAnnotationsLocalization(options =>
{
options.DataAnnotationLocalizerProvider = (type, factory) =>
factory.Create(typeof(ShareResource));
});
Startup Configure:启动配置:
var supportedCultures = new List<CultureInfo>()
{
new CultureInfo("en"),
new CultureInfo("de"),
new CultureInfo("fr"),
new CultureInfo("pt"),
new CultureInfo("tr")
};
var options = new RequestLocalizationOptions()
{
DefaultRequestCulture = new RequestCulture("en"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures,
RequestCultureProviders = new List<IRequestCultureProvider>()
{
new QueryStringRequestCultureProvider(),
new CookieRequestCultureProvider()
}
};
app.UseRequestLocalization(options);
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "defaultWithLang",
pattern: "{lang}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
And in HomeController :在HomeController 中:
public IActionResult ChangeLang(string lang)
{
Response.Cookies.Append(CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(lang)),
new CookieOptions(){
Expires = DateTimeOffset.UtcNow.AddYears(1)
});
return Redirect(Request.Headers["Referer"].ToString());
}
The URL I expected to show is like "http://domain/en/controller/action" but it is not changing by switching the language and is always like "http://domain/controller/action" without language code.我希望显示的 URL 类似于“http://domain/en/controller/action”,但它不会通过切换语言而改变,并且始终类似于没有语言代码的“http://domain/controller/action”。
Is there anything else I have to add to my code in startup or somewhere else?在启动或其他地方,我还有什么需要添加到我的代码中的吗? Appreciated for any help.
感谢任何帮助。
what happens hen you manually set url?当您手动设置 url 时会发生什么?
right now, you are using default fallback route.现在,您正在使用默认回退路线。
check this:检查这个:
routes.MapRoute(
name: "LocalizedDefault",
template: "{lang:lang}/{controller=Home}/{action=Index}/{id?}"
);
routes.MapRoute(
name: "default",
template: "{*catchall}",
defaults: new { controller = "Home", action = "RedirectToDefaultLanguage", lang = "en" });
you can also add lang
constraint您还可以添加
lang
约束
services.Configure<RouteOptions>(options =>
{
options.ConstraintMap.Add("lang", typeof(LanguageRouteConstraint));
});
then add following lines just above route configuration然后在路由配置上方添加以下几行
var options = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(options.Value);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.