[英]Netcore 2.2 Localized Routing - How to always redirect to default route
Successfully using the project laid out at Localized routing using ASP.NET Core MVC 2 however there are a few modifications I'd like to make and I'm not clear on how to go about. 使用ASP.NET Core MVC 2成功使用了本地化路由中布置的项目,但是我想进行一些修改,但不清楚如何进行。
Currently my start.cs looks like the below, this works fine however what it does is in the Default culture English, my route is www.site.com whereas when i switch to any other culture I get www.site.com/fr/accuel or www.site.com/es/casa... 目前,我的start.cs如下所示,但工作正常,但是在默认区域性英语中是有效的,我的路线是www.site.com,而当我切换到任何其他区域性时,我都可以访问www.site.com/fr/ Accel或www.site.com/es/casa ...
How can I get the Default Language to always appear as www.site.com/en/home 如何使默认语言始终显示为www.site.com/en/home
startup.cs startup.cs
// Set up cultures
LocalizationRouteDataHandler.DefaultCulture = "en";
LocalizationRouteDataHandler.SupportedCultures = new Dictionary<string, string>()
{
{ "en", "English" },
{ "fr", "Français" },
{ "es", "Español" }
};
And my HomeController 还有我的HomeController
[AllowAnonymous]
[LocalizationRoute("en", "home")]
[LocalizationRoute("fr", "accueil")]
[LocalizationRoute("es", "casa")]
public class HomeController : LocalizationController
{
[LocalizationRoute("en", "/home")]
[LocalizationRoute("fr", "/accueil")]
[LocalizationRoute("es", "/casa")]
public IActionResult Index()
{
return View();
}
For LocalizationRoute
, it defines the route template for MVC, which is used to map the request to the action. 对于
LocalizationRoute
,它定义了MVC的路由模板,该模板用于将请求映射到操作。
For default design, for request /
which will be routed to Home/Index
with english culture. 对于默认设计,对于请求
/
它将被路由到具有英语文化的Home/Index
。 If you prefer to show the url with /en/home
you need to redirect the url by code below: 如果您希望使用
/en/home
显示该网址,则需要通过以下代码重定向该网址:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
var options = new RewriteOptions()
.AddRedirect(@"^$", $"{LocalizationRouteDataHandler.DefaultCulture}/{LocalizationRouteDataHandler.DefaultController}");
app.UseRewriter(options);
var localizationOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(localizationOptions.Value);
//rest code
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}",
defaults: new { culture = LocalizationRouteDataHandler.DefaultCulture }
);
});
}
Note For above way, you need to keep [LocalizationRoute("en", "/home")]
to HomeController. 注意对于上述方法,您需要将
[LocalizationRoute("en", "/home")]
保留在HomeController中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.