简体   繁体   English

多语言 Asp.Net Core 3.1 MVC 应用程序未在 url 中显示语言代码

[英]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.

相关问题 asp.net 核心 3.1 web 应用程序中未显示的代码参考 - References of code not showing in asp.net core 3.1 web application 选定的复选框未显示在 asp.net core mvc 3.1 的 HttpPost 操作方法中 - Selected Checkboxes are not showing up in the HttpPost action method in asp.net core mvc 3.1 从ASP.NET MVC框架迁移到ASP.NET Core 3.1 MVC后图像不显示 - Image does not display after migration from ASP.NET MVC framework to ASP.NET Core 3.1 MVC 使用 Visual Studio 代码创建 Asp.net MVC 应用程序(非核心) - Create Asp.net MVC Application (not core) with visual studio code 如何在 asp.net 核心 3.1 mvc web 应用程序中将日志保存到数据库? - How do I save logs to database in asp.net core 3.1 mvc web application? 我应该如何保护我的 Web 应用程序(ASP.Net Core 3.1 MVC)? - How should i secure my Web Application(ASP.Net Core 3.1 MVC)? ASP.NET 5 MVC 6多语言应用程序 - ASP.NET 5 MVC 6 Multi Language Application 如何在 ASP.NET Core 3.1 MVC 中进行自定义路由 - How to do custom routing in ASP.NET Core 3.1 MVC 带有 Blazor 的 ASP.NET Core 3.1 MVC - 缺少程序集 - ASP.NET Core 3.1 MVC with Blazor - Missing Assembly 将 ASP.NET CORE 3.1 MVC 应用程序部署到 azure 时出现问题 - Problem deploying ASP.NET CORE 3.1 MVC app to azure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM