0

I guess I still don't understand routing.

I have three controllers

  • AdminController
  • DashboardController
  • ProjectGroupsController

For Dashboard, I want the url to be /Dashboard/. For Admin section, however, I want two different controllers. /Admin/Overview should be using AdminController, and /Admin/ProjectGroups/ should be using ProjectGroupsController.

This is how my routing looks like

 routes.MapRoute(
          name: "AdminOverivew",
           url: "Admin/Overview",
           defaults: new { controller = "Admin", action = "Overview" },
           namespaces: new[] { "Com.Controllers" }
        );

    routes.MapRoute(
      name: "AdminSubs",
       url: "Admin/{controller}/{action}/{id}",
       defaults: new { action = "Index", id = UrlParameter.Optional },
       namespaces: new[] { "Com.Controllers" }
    );

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional },
        namespaces: new[] {"Com.Controllers"}
    );

*Note, the reason for the 2nd route is because I have many more controllers under admin section.

It's working.. except, the URL that HTML.ActionLink generates is wrong. @Html.ActionLink("Dashboard", "Index", "Dashboard"........) generates /Admin/Dashboard when it should be /Dashboard.

However, @Html.ActionLink("Project Groups", "Index", "ProjecGroups".....) generates the correct url /Admin/ProjectGroups

If I take out the 2nd routes.MapRoute (AdminSubs), then the situation is reversed. Dashboard gets the right url, /Dashboard then Project Groups becomes /ProjectGroups when it should remain /Admin/ProjectGroups

What gives?

4

1 回答 1

1

我认为您需要一些更明确的路由,因为路由器不知道 和 的“全部捕获”之间的Admin/{controller}/{action}/{id}区别{controller}/{action}/{id}。第一个匹配的模式是它使用的模式。

您要么需要摆脱Admin/{controller}/{action}/{id}admin 前缀下的任何内容,都需要显式分配为路由,或者相反,删除{controller}/{action}/{id}路由,并显式创建应该在根目录之外的路由。

于 2013-07-17T19:35:09.960 回答