【问题标题】:Correct me on URL routing in MVC issue纠正我在 MVC 问题中的 URL 路由
【发布时间】:2015-03-25 03:56:05
【问题描述】:

这与我在此链接correct me on url routing in mvc 中提出的问题有关

现在我遇到了另一个问题,所以我想我会作为新问题提出。

现在我的 global.asax 文件中有以下路线

routes.MapRoute(
           "Custom", // Route name
           "{action}/{id}", // URL with parameters
           new { controller = "Authentication", action = "BigClientLogin", id = UrlParameter.Optional } // Parameter defaults
       );

routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Authentication", action = "BigClientLogin", id = UrlParameter.Optional } // Parameter defaults
        );

现在发生的情况是,当我运行我的解决方案时,我得到的 URL 是 http://localhost:65423/Login,这是我的登录页面所需要的,没问题。但是当我以用户身份登录时,我收到 “找不到资源” 错误。

当我检查它时,我可以看到我的 URL 现在更改为 http://localhost:65423/Admin/Dashboard

所以我认为这会导致问题。所以这看起来与我的global.asax 路由有关的问题。

谁能帮我找出我做错了什么。

【问题讨论】:

  • 假设您已经阅读了路由的工作原理(即ScottGu article)并查看了您之前问题的答案 - 您能否解释一下您对匹配相同路径集的 2 条路由的期望和代码的行为与您的预期有何不同?
  • @alexi 感谢您的回复。我的问题略有变化。我实际上对应该使用哪种路由格式感到困惑。实际上我的客户要求是他们的登录页面 url 应该看起来像 localhost:65423/Login 这个。所以我设法使用第一路线格式创建它。但是一旦我登录,由于第一路线格式,没有任何动作被识别。但是,如果我使用第二种路由格式,即使在登录后它也能正常工作。但是那个时候我的第一个条件不会满足。

标签: c# asp.net asp.net-mvc url-routing asp.net-mvc-routing


【解决方案1】:

它的发生是因为两件事。 第一个你的路线顺序 第二个你的默认值

因此,如果您最终使用不是默认设置的“管理员/仪表板”,猜猜您只是将它放在您的工作室中开始了吗?

要到达http://localhost:65423/Login,您需要在您的 AuthenticationController 上执行一个名为“Login”的操作,但看起来您配置的操作是“BicClientLogin”,因此除非您指定它,否则您不会被带到登录,并且在那个时候它必须存在。

为了进一步帮助您,我们需要知道您拥有哪些控制器以及存在哪些操作,以及安全性是否是您的解决方案的一部分,如果以防万一,将使用什么。

【讨论】:

    【解决方案2】:

    您有 2 条带有完全可选路段的路线。问题是路由框架无法区分它们。

    使它与现有路由一起工作的唯一方法是通过名称明确指定它们(例如使用@Html.RouteLink@Html.RouteUrl 时)。

    @Html.RouteLink("Custom Link 1", "Custom", new { action = "BigClientLogin" })
    @Html.RouteLink("Custom Link 2", "Custom", new { action = "Action2" })
    @Html.RouteLink("Home Page", "Default", new { controller = "Home", action = "Index" })
    @Html.RouteLink("About", "Default", new { controller = "Home", action = "About" })
    

    这样做会起作用,但不正常。通常,只有一个路由配置了控制器、操作和 id 的所有默认值,其余路由有一些明确声明的段和/或约束(段更可取)。

    routes.MapRoute(
       "Custom", // Route name
       "Custom/{action}/{id}", // URL with parameters
       new { controller = "Authentication", action = "BigClientLogin", id = UrlParameter.Optional } // Parameter defaults
    );
    
    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Authentication", action = "BigClientLogin", id = UrlParameter.Optional } // Parameter defaults
    );
    

    现在只有当 URL 以 /Custom/ 开头时,第一个路由才会匹配。如果不以custom开头,则匹配默认路由。

    诀窍是ensure that the routes are listed in the right order and that they only match the URL in specific cases,如果情况不正确,让他们传递到列表中的下一条路线。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多