如果未在网址中指定区域性,则重定向到默认区域性

时间:2018-12-12 10:04:37

标签: asp.net-core-mvc asp.net-core-2.0 asp.net-core-localization routelocalization-mvc

我正在尝试执行类似于Microsoft用于文档站点的操作。 例如,当我在地址栏中输入下面的链接时,Microsoft docs网站会将我重定向到默认的文化链接,如第二个链接中一样。

第一个链接:https://docs.microsoft.com/aspnet/core/fundamentals/localization?view=aspnetcore-2.2 第二个链接:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-2.2

我逐步使用this article (https://joonasw.net/view/aspnet-core-localization-deep-dive)并成功运行了项目,并且可以通过调用

这样的网址来更改区域性
localhost:5001/tr-TR/home/index ({culture}/[controller]/[action]) 

但是当我尝试路由到

localhost:5001/home/index (without culture route value)

我收到错误404。

我需要的是将上述链接路由到某些共享控制器操作中,然后将其重定向到

localhost:5001/{defaultculture}/home/index

先谢谢您。

2 个答案:

答案 0 :(得分:0)

您需要更改路线。在下面的示例中,当用户输入localhost:5001/home/index时,将在RedirectToCulture控制器中重定向到Home动作。现在,您可以阅读(或不可以)阅读参数,并使用聋人文化进行重定向

routes.MapRoute(
    name: "default1",
    template: "{culture:required}/{controller:required}/{action:required}/{id?}",
    defaults: new { culture = "en-US", controller = "Home", action = "Index" });

routes.MapRoute(
    name: "default2",
    template: "{stringController?}/{stringAction?}/{id?}",
    defaults: new { controller = "Home", action = "RedirectToCulture" });

答案 1 :(得分:0)

对于https://docs.microsoft.com,它使用URL重定向功能来实现从https://docs.microsoft.com/aspnet/core/fundamentals/localization?view=aspnetcore-2.2https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-2.2的路由。如果检查https://docs.microsoft.com/aspnet的请求日志,则会发现第一个请求,它返回带有Status Code: 301的响应,然后返回https://docs.microsoft.com/en-us/aspnet

根据您的要求,如果您只需要localhost:5001/home/index即可从localhost:5001/en-us/home/index返回视图而无需将URL更改为localhost:5001/en-us/home/index。您可以尝试

var localizationOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>().Value;
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?}");
        });
    });

});

如果您需要与localhost:5001/en-us/home/index相同的URL,则还需要执行重定向操作。

app.Use(async (context, next) =>
{
    await next.Invoke();
    if (context.Response.StatusCode == StatusCodes.Status404NotFound)
    {
        context.Response.Redirect(@"/en-us/Home/Index", true);
    }
});

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

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

});