【问题标题】:Mvc: route config order routesmvc: route config 命令路由
【发布时间】:2013-04-09 16:26:59
【问题描述】:

我已经添加了这样的自定义路线

        routes.MapRoute(
          name: "Default",
          url: "{coutry}/{lang}/{controller}/{action}",
          defaults: new { controller = "Home", action = "Index" }
        );

现在当我尝试从一个控制器调用方法时遇到一些问题,这在添加新路由之前运行良好

<a id="someId" class="link-button" href="../Documents/Create"><span>Create</span></a>

现在我能做到这一点的唯一方法是使用 href="EN/us/Documents/Create" 之类的东西

有没有办法为我的客户端保留自定义路由,并且仍然为我的管理端保留 href="../Documents/Create"&gt; 方式,这是因为我在管理端开发了几个功能,但现在我必须包含该自定义客户端的路由。非常感谢。

现在有我的路线

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

routes.MapRoute(
            name: "CustomRoute",
            url: "{country}/{lang}/{controller}/{action}",
            defaults: new { controller = "Test", action = "Index" }
        );

但我只能使用 /ES/es/Test/Index 访问 CustomRoute ...为什么不采用默认值?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 routes


    【解决方案1】:

    您只需默认路由之后声明您的自定义路由:

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index",
            id = UrlParameter.Optional }
    );
    // SomeOther = someothercontroller
    routes.MapRoute(
        name: "CustomRoute",
        url: "{coutry}/{lang}/{controller}/{action}",
        defaults: new { controller = "SomeOther", action = "Index" }
    );
    

    【讨论】:

    • 默认路由配置应始终位于路由配置的末尾。由于路由配置将解析匹配配置,因此它将接受并路由到该路径。因此,如果您在开始时指定默认配置,它将始终匹配并路由到该路径。
    • 我知道这是我的第一个答案,请参阅我的编辑,但仔细查看他的示例并非如此。
    • 非常感谢您的帮助,请查看我的编辑并告诉我您是否知道发生了什么...
    • 好的,以确保我没有给你一个错误的答案,至少是可行的,我测试了我自己的代码。所以我有我给你的路线配置和这个:&lt;a id="someId" class="link-button" href="../Documents/Create"&gt;&lt;span&gt;Create&lt;/span&gt;&lt;/a&gt; &lt;a class="link-button" href="EN/us/someother/testmethod2"&gt;&lt;span&gt;Go to Method 2&lt;/span&gt;&lt;/a&gt;。两种方法都被调用。那么why is not taking default values 是什么意思?
    • 好吧,你是对的,所以我想告诉你的是,CustomRoute 是我的索引,当我尝试使用http://localhost:xxxx/Es/es/ 访问时,我得到 404 错误,它没有被 SomeOther 控制器和我们在默认值中定义的索引操作,我只能通过http://localhost:xxxx/Es/es/SomeOther/Index 获得访问权限,我需要第一种方式。谢谢。
    【解决方案2】:

    您已将默认 RouteConfig 替换为新配置,并且它确实匹配此 {coutry}/{lang}/{controller}/{action} 格式的 Url。

    如果你想接受../Documents/Create Url 你必须在末尾添加默认的RouteConfig。

    routes.MapRoute(
      name: "CustomRoute",
      url: "{coutry}/{lang}/{controller}/{action}",
      defaults: new { controller = "Documents", action = "Index" }
    );
    
    routes.MapRoute(
      name: "Default",
      url: "{controller}/{action}/{id}",
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
    

    同样在锚标记&lt;a id="someId" class="link-button" href="../Documents/Create"&gt;&lt;span&gt;Create&lt;/span&gt;&lt;/a&gt; 中而不是硬编码href,您可以编写如下内容。

    &lt;a id="someId" class="link-button" href="@Url.Action("Create","Documents")&gt;&lt;span&gt;Create&lt;/span&gt;&lt;/a&gt;

    【讨论】:

    • 感谢您的帮助,它仍然不适合我,请查看我的编辑并告诉我您是否知道发生了什么?
    • @Steve :更新了我的答案,请尝试。
    • 你是对的,我尝试了你的答案,非常感谢,但现在我遇到的问题是我只能使用 /ES/es/Test/Index 访问 CustomRoute,它是用于主页我只需要使用 /Es/es 来访问它,有一些想法吗?
    • 您的默认Controlleraction 名称是什么?带有/Es/esUrl的请求应该重定向到哪里?
    • 这现在是 routes.MapRoute( name: "CustomRoute", url: "{country}/{lang}/{controller}/{action}", defaults: new { controller = "Test", action = "Index" } );,带有测试控制器和索引操作,但是当 /Es/es Url 时我仍然收到 404 错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-30
    相关资源
    最近更新 更多