ASP.Net MVC路由问题 - 问号出现在我的链接上

时间:2009-07-23 11:49:41

标签: asp.net-mvc

我有一个接受页面名称的公共网站,然后默认使用控制器和操作,并使用页面名称作为唯一标识符来呈现正确的视图。例如。 http://www.mydomain.com/homepage

我还有一个管理区域,所有CRUD内容都通过admin的前缀进行访问。例如。 http://www.mydomain.com/admin/controller/action

一切都很好,直到最近我改变了一些东西,现在当我得到http://www.mydomain.com/homepage我所拥有的链接时,例如:

<ul id="menu">              
   <li><%= Html.ActionLink("Home", "Details", "WebPage", new { pageName = "homepage" }, null)%></li>
   <li><%= Html.ActionLink("About", "Details", "WebPage", new { pageName = "homepage" }, null)%></li>                    
</ul>

不再显示为http://www.mydomain.com/homepage,而是http://www.mydomain.com/Admin/WebPage/Details?pageName=homepage

有人可以帮忙吗?

这是我的Global.asax:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute("AdminRoot",
            "Admin",
            new { controller = "Admin", action = "Index" }
            );

        routes.MapRoute(
            "LogOn",                                              // Route name
            "LogOn",                           // URL with parameters
            new { controller = "Account", action = "LogOn" },
            new { action = "LogOn" }
        );

        routes.MapRoute("Account",
          "Account/{action}",
          new { controller = "Account", action = "" }
          );

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


        routes.MapRoute(
        "ErrorRoute",                                              // Route name
        "Error/Error404",                           // URL with parameters
        new { controller = "Error", action = "Error404" }
        );


        routes.MapRoute("Admin",
            "Admin/{controller}/{action}/{id}",
            new { controller = "Admin", action = "Index", id = "" }
            /*,new { action = "Create|Edit|Delete" }*/
            );

        routes.MapRoute("EventNewsData",
           "Admin/{controller}/{action}/{year}/{month}",
           new { controller = "Admin", action = "Index", year = 0, month = 0 }
            /*,new { action = "Create|Edit|Delete" }*/
           );

        routes.MapRoute(
            "Default",                                              // Route name
            "{pageName}/{moreInfoID}",                           // URL with parameters
            new { controller = "WebPage", action = "Details", pageName = "homepage", moreInfoID = 0 },
            new { action = "Details" }
            );


        routes.MapRoute("Error", "{*url}", new { controller = "Error", action = "Error404" });

    }

更新:这已经解决了,但不确定为什么

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute("AdminRoot",
            "Admin",
            new { controller = "Admin", action = "Index" },
            new { action = "Index" }
            );

        routes.MapRoute(
            "LogOn",                                              // Route name
            "LogOn",                           // URL with parameters
            new { controller = "Account", action = "LogOn" },
            new { action = "LogOn" }
        );

        routes.MapRoute("Account",
          "Account/{action}",
          new { controller = "Account", action = "" }
          );

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





        routes.MapRoute("Admin",
            "Admin/{controller}/{action}/{id}",
            new { controller = "Admin", action = "Index", id = "" }
            , new { action = "Create|Edit|Delete|Index|DeleteFromIndex" }
            );

        routes.MapRoute("EventNewsData",
           "Admin/{controller}/{action}/{year}/{month}",
           new { controller = "Admin", action = "Index", year = 0, month = 0 }
            , new { action = "GetCalendarData" }
           );

        routes.MapRoute(
            "Default",                                              // Route name
            "{pageName}/{moreInfoID}",                           // URL with parameters
            new { controller = "WebPage", action = "Details", pageName = "homepage", moreInfoID = 0 },
            new { action = "Details" }
            );

        routes.MapRoute(
        "ErrorRoute",                                              // Route name
        "Error/Error404",                           // URL with parameters
        new { controller = "Error", action = "Error404" }
        );

        routes.MapRoute("Error", "{*url}", new { controller = "Error", action = "Error404" });

    }

1 个答案:

答案 0 :(得分:2)

问题可能出在action = "Details"约束中(“{pageName} / {moreInfoID}”中没有“{action}”):

routes.MapRoute(
        "Default",                                              // Route name
        "{pageName}/{moreInfoID}",                           // URL with parameters
        new { controller = "WebPage", action = "Details", pageName = "homepage", moreInfoID = 0 },
        new { action = "Details" }
        );

<强>更新:

现在你的代码正在使用这条路线:

routes.MapRoute("Admin",
        "Admin/{controller}/{action}/{id}",
        new { controller = "Admin", action = "Index", id = "" }
        /*,new { action = "Create|Edit|Delete" }*/
        );

但您可以使用Html.RouteLink代替:

<ul id="menu">              
    <li><%= Html.RouteLink("Home", "Default", new { pageName = "homepage" })%> </li>
    <li><%= Html.RouteLink("About", "Default", new { pageName = "homepage" })%> </li>                    
</ul>

<强>更新:

ASP.NET路由查找具有“详细信息”操作的路由和“WebPage”控制器(“pageName”是可选的)并匹配“Admin”路由。

<强>已更新

或者在“管理员”路线之前添加此路线:

routes.MapRoute("TheRoute",
    "{pageName}/{moreInfoID}",
    new { controller = "WebPage", action = "Details", moreInfoID = 0 },
    new { pageName = "homepage" }
);