asp.net-mvc - mvc4 区域内的路由操作无法映射操作

标签 asp.net-mvc c#-4.0 asp.net-mvc-4 asp.net-mvc-routing

我在尝试让路线在我拥有的区域工作时遇到问题。

我的区域称为 ABC,我在该区域内有一个称为 Home 的 Controller 。如果我浏览“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。我究竟做错了什么?

任何帮助都会很棒!

最佳答案

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

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/详细信息

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

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

关于asp.net-mvc - mvc4 区域内的路由操作无法映射操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10629894/

相关文章:

sql-server - sql 检查字符串是否包含 where 子句

asp.net - MVC Windows 身份验证提示输入 Windows 凭据并忽略自定义错误配置

c# - 何时转换 ValueProvider 的 ASP.NET 值?

asp.net-mvc - MVC4 站点发布到 Somee

asp.net-mvc - SignalR:无法连接到本地或任何其他 IP 地址

c# - 如何订购列表<string>

asp.net-mvc - IIS7 上的 MVC 4 网站 - 因细微更改而中断(AutoMapper?)

asp.net - 为了争论,我将如何说服某人 Linq2Sql 比 Asp.Net MVC 项目中的数据访问应用程序 block 好得多?

asp.net-mvc - ASP.NET MVC 中的 LinkBut​​ton

linq-to-sql - Linq 到 SQL : Left joining a grouped set makes a Cross Apply/Outer Apply