3

我在尝试让路线在我拥有的地区工作时遇到了麻烦。

我的区域称为 ABC,我在该区域内有一个名为 Home 的控制器。如果我浏览“http://localhost:8000/abc”,我可以使用 Home/Index 打断点,但是当我尝试点击另一个名为“http://localhost:8000/ABC/details”之类的详细信息的操作时,我得到了一个404。

我试过了

context.MapRoute(
           "details",
           "ABC/Home/{action}/{id}",
           new { action = "details", id = UrlParameter.Optional },
             constraints: null,
           namespaces: new[] { "WebApplication.Areas.ABC.Controllers" }

       );



        context.MapRoute(
          "ABC_Home",
          "ABC/{controller}/{action}/{id}",
          new { controller = "home",action="Index", id = UrlParameter.Optional },
            constraints: null,
            namespaces: new[] { "WebApplication.Areas.ABC.Controllers" }
      );

如果我使用“http://localhost:8000/ABC/Home/Details”,这允许我执行操作

 context.MapRoute(
           "details",
           "Home/Home/{action}/{id}",
           new {controller="home", action = "details", id = UrlParameter.Optional },
             constraints: null,
           namespaces: new[] { "WebApplication.Areas.ABC.Controllers" }

       );

理想情况下,如果可能的话,我不想在 url 中使用 home 。我究竟做错了什么?

任何帮助都是极好的!

4

2 回答 2

3

我认为您只需要一条路线即可。不要在路由中包含控制器,因为它似乎以/ABC开头;只需将控制器分配为默认值:

context.MapRoute(
    "ABC_Home",
    "ABC/{action}/{id}",
    new { controller = "home", action="Index", id = UrlParameter.Optional },
    constraints: null,
    namespaces: new[] { "WebApplication.Areas.ABC.Controllers" }
}

根据您的要求,这会将/abc路由到/home/index,并将/abc/details路由到/home/details

然后,如果您需要访问其他控制器,您可以为此添加另一个规则,类似于默认规则:

context.MapRoute(
    "Default_Route",
    "{controller}/{action}/{id}",
    new { id = UrlParameter.Optional }
}
于 2012-05-17T04:44:58.943 回答
0

我不认为你可以默认一个具有可变动作名称的控制器,否则无法从路由中判断它是一个动作或控制器以及匹配哪个路由。我认为您可以对操作进行硬编码:

Context.MapRoute(
    "ABC_Home_Details",
    "ABC/Details/{id}",
    new { controller = "home", action="details", id = UrlParameter.Optionsl },
    constraints: null,
    namespaces: new [] { "WebApplication.Areas.ABC.Controllers" }
);
于 2012-05-17T04:51:04.750 回答