2012-05-17 34 views
3

我在尝试获取在我所在的区域中工作的路由时遇到问题。mvc4区域内的路由动作无法映射动作

我的地区叫做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://本地主机:8000/ABC /主页/详细信息” 我打的动作

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

     ); 

理想如果可能的话,我不想在网址中使用家庭。我究竟做错了什么?

任何帮助将会很棒!

回答

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/首页/指数,并且将路由/ABC /细节/家/细节

然后,如果你需要访问其他控制器,你可以添加其他规则是,像那些默认

context.MapRoute(
    "Default_Route", 
    "{controller}/{action}/{id}", 
    new { id = UrlParameter.Optional } 
} 
+0

谢谢dbaseman –

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" } 
);