定义特定网址的路由

问题描述

我的ASP.NET MVC 5项目是多语言的,我使用文化来更改语言。

我已经定义了默认的区域性路由,如下所示:

public static void RegisterRoutes(RouteCollection routes)
{
    var _cul = CultureHelper.GetCurrentCulture() != null
            ? CultureHelper.GetCurrentCulture()
            : ConfigurationSettings.AppSettings["WebSiteDefaultCulture"];

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapMvcAttributeRoutes();

    routes.MapRoute(
            "Default","{culture}/{Controller}/{action}/{id}",new {culture = _cul,controller = "Home",action = "Index",id = UrlParameter.Optional}
        );
    routes.MapRoute(
            "Home","{culture}/Home/{action}/{id}",new {action = "Index",id = UrlParameter.Optional}
        );
}

因此,将仅以区域性打开网站所有页面的URL。

我想打开一些没有文化的特定网站URL。

例如,可以在不提供区域性的情况下打开此URL https://test.com/en-US/Cart/Cart/5

谢谢。

解决方法

解决方案是定义两个路由,带和不带文化

routes.MapRoute(
            name: "DefaultWithCulture",url: "{culture}/{controller}/{action}/{id}",defaults: new { controller = "Home",action = "Index",id = UrlParameter.Optional },constraints: new { culture = _cul }
            );

            routes.MapRoute(
                name: "Default",url: "{controller}/{action}/{id}",defaults: new { culture = _cul,controller = "Home",id = UrlParameter.Optional }
            );

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...