这是我的路线:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "defaultWithCulture",
template: "{culture=fa-IR}/{controller}/{action=Index}/{id?}");
});
但是当我使用链接时本地化工作:
http://localhost:1776/fa-IR/Home/About?culture=fa-ir
但不是
http://localhost:1776/fa-IR/Home/About
为什么路线中的文化不适用?
答案 0 :(得分:4)
要做的是:
await localTable.UpdateAsync(serverItem);
所以我们必须改变它们的顺序并将路由添加为第一个:
QueryStringRequestCultureProvider
CookieRequestCultureProvider
AcceptLanguageHeaderRequestCultureProvider
2-执行RouteDataRequestCultureProvider任务时,仍然路由数据为空并且尚未给出值,因此它将始终返回null,因此我将其更改为如下所示使用services.Configure<RequestLocalizationOptions>(
options =>
{
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-US"),
new CultureInfo("fa-IR"),
};
options.DefaultRequestCulture = new RequestCulture(culture: "fa-IR", uiCulture: "fa-IR");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.RequestCultureProviders.Clear();
options.RequestCultureProviders.Insert(0, new RouteDataRequestCultureProvider();
options.RequestCultureProviders.Insert(1, new QueryStringRequestCultureProvider());
options.RequestCultureProviders.Insert(2, new CookieRequestCultureProvider());
options.RequestCultureProviders.Insert(3, new AcceptLanguageHeaderRequestCultureProvider());
services.AddSingleton(options);
});
代替RoutingData:
Url.Path
答案 1 :(得分:3)
据我所知,asp.net核心依次有以下RequestCultureProviders。首先使用非null。
QueryStringRequestCultureProvider CookieRequestCultureProvider AcceptLanguageHeaderRequestCultureProvider
您需要添加RouteDataRequestCultureProvider以支持来自路线数据的文化。
从here获取示例:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("en-GB"),
new CultureInfo("de"),
new CultureInfo("fr-FR"),
};
var options = new RequestLocalizationOptions()
{
DefaultRequestCulture = new RequestCulture(culture: "en-GB", uiCulture: "en-GB"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
};
options.RequestCultureProviders = new[]
{
new RouteDataRequestCultureProvider() { Options = options }
};
services.AddSingleton(options);
}