У меня есть веб-приложение для бритвы Pages (vs2019, .net core 3.0). Я хочу локализовать URL-адреса, такие как "/ fi-fi / Contact" "/ en-US / Contact"
Я использовал "DynamicRouteValueTransformer" с некоторым успехом, единственная проблема - когда я печатаю "/ en-US /Индекс "для адресной строки.
public class LanguageTransformer : DynamicRouteValueTransformer
{
public override ValueTask<RouteValueDictionary> TransformAsync(HttpContext httpContext, RouteValueDictionary values)
{
if (!values.ContainsKey("language") || !values.ContainsKey("page"))
return new ValueTask<RouteValueDictionary>(values);
var x = values["page"];
values["page"] = "/" + x;
return new ValueTask<RouteValueDictionary>(values);
}
}
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(o =>
{
// We will put our translations in a folder called Resources
o.ResourcesPath = "Resources";
});
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("fi-FI"),
new CultureInfo("en-US"),
};
options.DefaultRequestCulture = new RequestCulture(culture: "fi-FI", uiCulture: "fi-FI");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
// get email settings
services.Configure<EmailSettings>(Configuration.GetSection("EmailSettings"));
// PWA
services.AddSingleton(typeof(IHttpContextAccessor), typeof(HttpContextAccessor));
// EMail render service
services.AddScoped<IViewRenderService, ViewRenderService>();
services.AddSingleton<LanguageTransformer>();
services.AddRazorPages()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
services.AddTransient<IMailService, EMailService>();
//MailKit
services.AddSingleton<IEMailServiceUsingMailKit, EMailServiceUsingMailKit>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/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();
}
var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
//
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapDynamicPageRoute<LanguageTransformer>("{language}/{page}");
endpoints.MapRazorPages();
});
}
}
}
Произошло необработанное исключение при обработке запроса.
AmbiguousMatchException: запрос соответствует нескольким конечным точкам. Совпадения:
/ Индекс / Индекс
Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ReportAmbiguity (CandidateState [] кандидатState)