ASP.NET Core 5.0 RouteDataRequestCultureProvider 删除 url 中的默认文化 [英] ASP.NET Core 5.0 RouteDataRequestCultureProvider remove default culture in url
问题描述
我尝试向我的 asp.net-core 项目添加多语言功能,但在 RequestLocalization 中 .net 3.1 和 5.0 之间有一些变化,我无法得到我想要的.我为每种语言添加了 Resource 文件,并在我的 razor 页面中使用了 Resource,它可以工作,但存在一个不需要的默认路由错误,我希望我的路由对默认文化友好.
这就是我想要的,
对于默认文化(土耳其语):
site.com/foosite.com/foo/barsite.com/foo/bar/5
对于非默认文化(英语):
site.com/en/foosite.com/en/foo/barsite.com/en/foo/bar/5
我的另一个问题是;我的项目将 site.com/foo/foo/bar 这个 url 呈现为像 site.com/tr/foo/bar 这样不行,我想它应该重定向到 404 页面.
我的启动示例代码如下:
public void ConfigureServices(IServiceCollection services){services.AddResponseCompression();services.AddLocalization(opts => opts.ResourcesPath = "Resources");services.Configure(options =>{var supportedCultures = new[]{new CultureInfo("tr-TR"),new CultureInfo("en")};options.DefaultRequestCulture = new RequestCulture("tr");options.SupportedCultures = supportedCultures;options.SupportedUICultures = supportedCultures;options.RequestCultureProviders.Insert(0, new RouteDataRequestCultureProvider());});services.AddControllersWithViews();services.AddRazorPages();services.AddRouting(options => options.LowercaseUrls = true);}公共无效配置(IApplicationBuilder 应用程序,IWebHostEnvironment 环境){app.UseResponseCompression();如果 (env.IsDevelopment())app.UseDeveloperExceptionPage();别的{app.UseExceptionHandler("/Home/Error");//默认的 HSTS 值为 30 天.您可能希望针对生产场景更改此设置,请参阅 https://aka.ms/aspnetcore-hsts.app.UseHsts();}app.UseHttpsRedirection();app.UseStaticFiles();app.UseRouting();var supportedCultures = new string[] { "tr-TR", "en";};app.UseRequestLocalization(options =>选项.AddSupportedCultures(supportedCultures).AddSupportedUICultures(supportedCultures).SetDefaultCulture(tr-TR").RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(context => Task.FromResult(new ProviderCultureResult(tr-TR")))));app.UseAuthorization();app.UseEndpoints(endpoints =>{endpoints.MapControllerRoute(name: "culture-route", pattern: "{culture}/{controller=Home}/{action=Index}/{id?}");endpoints.MapControllerRoute(name: "default", "{culture=tr}/{controller=Home}/{action=Index}/{id?}");});}
Razor 资源使用和文化变化导航
资源文件
我该如何解决这个问题或者我做错了什么?
编辑
我发现了
英语 ->
法语 ->
你可以问我问题并享受本地化的乐趣:)
I tried to add multi language feature to my asp.net-core project but there are some changes between .net 3.1 and 5.0 in RequestLocalization and i couldn't get what i want. I added Resource files for each language and I used Resource in my razor pages, its working but there is one unwanted default route bug and i want my routing to work friendly for default culture.
This is what i want,
For default culture (Turkish):
site.com/foo
site.com/foo/bar
site.com/foo/bar/5
For non-default culture (English):
site.com/en/foo
site.com/en/foo/bar
site.com/en/foo/bar/5
My other problem is; My project renders site.com/foo/foo/bar this url like site.com/tr/foo/bar it's not okay and i guess it should redirect to 404 page.
My Startup sample code below:
public void ConfigureServices(IServiceCollection services)
{
services.AddResponseCompression();
services.AddLocalization(opts => opts.ResourcesPath = "Resources");
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("tr-TR"),
new CultureInfo("en")
};
options.DefaultRequestCulture = new RequestCulture("tr");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.RequestCultureProviders.Insert(0, new RouteDataRequestCultureProvider());
});
services.AddControllersWithViews();
services.AddRazorPages();
services.AddRouting(options => options.LowercaseUrls = true);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseResponseCompression();
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.UseRouting();
var supportedCultures = new string[] { "tr-TR", "en" };
app.UseRequestLocalization(options =>
options
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures)
.SetDefaultCulture("tr-TR")
.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(context => Task.FromResult(new ProviderCultureResult("tr-TR"))))
);
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(name: "culture-route", pattern: "{culture}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(name: "default", "{culture=tr}/{controller=Home}/{action=Index}/{id?}");
});
}
Razor Resource usage and culture change navs
Resource files
How can I solve this or what am I doing wrong?
EDIT
I found this approach. It's using CookieRequestCultureProvider and there is no culture info in url but at least there are no corupted urls. I don't know if this is okay for SEO.
You need to configure localization in ASP.Net Core a little bit different for this purpose.
I have created new ASP.Net Core MVC
project and do the following steps:
- First of all, you need to create custom
UrlRequestCultureProvider
public class UrlRequestCultureProvider : RequestCultureProvider
{
private static readonly Regex PartLocalePattern = new Regex(@"^[a-z]{2}(-[a-z]{2,4})?$", RegexOptions.IgnoreCase);
private static readonly Regex FullLocalePattern = new Regex(@"^[a-z]{2}-[A-Z]{2}$", RegexOptions.IgnoreCase);
private static readonly Dictionary<string, string> LanguageMap = new Dictionary<string, string>
{
{ "en", "en-US" },
{ "fr", "fr-FR" }
};
public override Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
var parts = httpContext.Request.Path.Value.Split('/');
// Get culture from path
var culture = parts[1];
if (parts.Length < 3)
{
return Task.FromResult<ProviderCultureResult>(null);
}
// For full languages fr-FR or en-US pattern
if (FullLocalePattern.IsMatch(culture))
{
return Task.FromResult(new ProviderCultureResult(culture));
}
// For part languages fr or en pattern
if (PartLocalePattern.IsMatch(culture))
{
var fullCulture = LanguageMap[culture];
return Task.FromResult(new ProviderCultureResult(fullCulture));
}
return Task.FromResult<ProviderCultureResult>(null);
}
}
- In
ConfigureServices()
add this code:
services.AddControllersWithViews().AddViewLocalization();
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCulters = new List<CultureInfo>()
{
new CultureInfo("en-US"),
new CultureInfo("fr-FR")
};
options.DefaultRequestCulture = new RequestCulture(supportedCulters.FirstOrDefault());
options.SupportedCultures = supportedCulters;
options.SupportedUICultures = supportedCulters;
options.RequestCultureProviders.Insert(0, new UrlRequestCultureProvider()
{
Options = options
});
});
- In
Configure()
add this code:
var requestLocalizationOptions = app.ApplicationServices.GetRequiredService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(requestLocalizationOptions.Value);
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "culture",
pattern: "{culture}/{controller=Home}/{action=Index}/{id?}");
});
- Also, I added
Resources
for en-US and fr-FR localization. More information about Resource file naming in Microsoft documentation.
Views.Home.Index.en-US.resx
Views.Home.Index.fr-FR.resx
- Finally, this is my Home view
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h1 class="display-4">@Localizer["Welcome"]</h1>
</div>
The results you can see on the screenshots.
Defaul ->
English ->
French ->
You can ask me questions and have fun with localizations :)
这篇关于ASP.NET Core 5.0 RouteDataRequestCultureProvider 删除 url 中的默认文化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!