我应该如何解决 ASP.Net MVC 路由映射冲突?

发布于 2024-09-12 07:30:31 字数 936 浏览 3 评论 0原文

我的 MVC 应用程序中映射了两条路线:

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Project",
        "{controller}/{projectid}/{action}/{id}",
        new { controller = "Project", action = "Index", id = "" });

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

}

在大多数情况下,这些路线不会造成任何问题,但在尝试调用 /Account/LogOn 视图时,我确实遇到了麻烦。我的 ActionLink 语法如下所示(我尝试了几种变体,包括空路由值等):

<%= Html.ActionLink("Log On", "LogOn", "Account") %>

浏览此视图会引发 404 错误(“找不到资源”)。理想情况下,我希望避免大量返工,那么避免这些冲突的最佳方法是什么?

I have two routes mapped in my MVC application:

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Project",
        "{controller}/{projectid}/{action}/{id}",
        new { controller = "Project", action = "Index", id = "" });

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

}

For the most part, these pose no problems, but I do strike trouble when trying to call the /Account/LogOn view. My ActionLink syntax looks like this (I've tried a couple of variations including null route values and such):

<%= Html.ActionLink("Log On", "LogOn", "Account") %>

Browsing to this view raises a 404 error ("The resource cannot be found"). Ideally I'd like to avoid a huge amount of rework, so what would be the best way to avoid these clashes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱要勇敢去追 2024-09-19 07:30:31

您是否尝试过使用控制器的文字进行这种变体?

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Project",
        "Project/{projectid}/{action}/{id}",
        new { controller = "Project", action = "Index", id = "" });

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

这会捕获: http://localhost/Project/12http://localhost/Project/12/Edithttp://localhost/Project/2/View/2

但它会通过 http ://localhost/Account/LogOn 到第二个路径。是的?

Have you tried this variation with the literal for the controller?

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Project",
        "Project/{projectid}/{action}/{id}",
        new { controller = "Project", action = "Index", id = "" });

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

This would catch: http://localhost/Project/12 and http://localhost/Project/12/Edit and http://localhost/Project/2/View/2

But it would pass http://localhost/Account/LogOn to the second rounte. Yes?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文