asp.net core 2.2 Url.Action忽略了活跃的文化

时间:2019-03-13 14:55:55

标签: asp.net-core

我有实现请求本地化的asp.net核心mvc 2.2应用程序。这是我的startup.cs

public void ConfigureServices(IServiceCollection services)
    {

        services.AddLocalization(options => { options.ResourcesPath = "Resources"; });
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        services.AddIdentity<ApplicationUser, ApplicationRole>().AddUserManager<ApplicationUserManager>()
            .AddUserStore<ApplicationUserRoleStore>().AddSignInManager<ApplicationSignInManager>().AddRoleStore<ApplicationUserRoleStore>().AddRoles<ApplicationRole>()
            .AddDefaultTokenProviders();

       services.AddAuthentication(sharedOptions =>
        {
            sharedOptions.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            sharedOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
       }).AddCookie();
        services.ConfigureApplicationCookie(options => { options.LoginPath = "/en-US/Login"; });
        services.AddTransient<IBusinessMailRepository, BusinessMailRepository>();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseAuthentication();

        IList<CultureInfo> supportedCultures = new List<CultureInfo>
        {
            new CultureInfo("en-US"),
            new CultureInfo("cs-CZ"),
        };
        var localizationOptions = new RequestLocalizationOptions
        {
            DefaultRequestCulture = new RequestCulture("en-US"),
            SupportedCultures = supportedCultures,
            SupportedUICultures = supportedCultures
        };

        var requestProvider = new RouteDataRequestCultureProvider();
        localizationOptions.RequestCultureProviders.Insert(0, requestProvider);

        app.UseRouter(routes =>
        {
            routes.MapMiddlewareRoute("{culture=en-US}/{*mvcRoute}", subApp =>
            {
                subApp.UseRequestLocalization(localizationOptions);

                subApp.UseMvc(mvcRoutes =>
                {
                    mvcRoutes.MapRoute(
                        name: "default",
                        template: "{culture=en-US}/{controller=Home}/{action=Index}/{id?}");
                });
            });
        });
    }

问题在于大多数@ Url.Action()调用会忽略URL中包含的活动区域性。例如-我请求localhost:12345 / cs-cz / ControllerName / ActionName,页面上生成的大多数超链接都具有localhost:12345 / zh-cn / ...默认区域性。

我需要所有Url.Action调用根据当前选择的区域性生成URL,这些URL包含在原始URL中。换句话说,我需要在请求之间保留一个选定的文化标识符。

我在asp.net MVC 5中已经习惯了这种方法,该技术确实只需要一个路由映射,并且无需额外配置即可很好地工作。

有人可以帮我吗? 谢谢。

0 个答案:

没有答案