我在尝试让路线在我拥有的区域工作时遇到问题。
我的区域称为 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/