两个不同区域的不同控制器同名导致路由冲突
我有两个区域:
ControlPanel
和 Patients
。
两者都有一个名为 ProblemsController 的控制器,它们仅在名称上相似。期望的结果将是产生 /controlpanel/problems => 的路线。 MyApp.Areas.ControlPanel.Controllers.ProblemsController 和 /患者/问题 => MyApp.Areas.Patients.Controllers.ProblemsController。
每个路由的配置如下:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"**Area Name Here**_default",
"**Area Name Here**/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
其中*****此处的区域名称***** 是控制面板或患者。
当我转到 /患者/问题/创建(例如)时,我收到 404,其中路由错误显示: 在控制器 'MyApp.Areas.ControlPanel.Controllers.ProblemsController 上找不到公共操作方法 'create' '
。
我不确定我做错了什么。
I have two areas:
ControlPanel
and Patients
.
Both have a controller called ProblemsController that are similar in name only. The desired results would be routes that yield /controlpanel/problems => MyApp.Areas.ControlPanel.Controllers.ProblemsController and /patients/problems => MyApp.Areas.Patients.Controllers.ProblemsController.
Each has routes configured like this:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"**Area Name Here**_default",
"**Area Name Here**/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
where *****Area Name Here***** is either ControlPanel or Patients.
When I go to /patients/problems/create (for example), I get a 404 where the routing error says: A public action method 'create' was not found on controller 'MyApp.Areas.ControlPanel.Controllers.ProblemsController'
.
I'm not sure what I'm doing wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实上,问题出在 Autofac 2.1 的 AutofacControllerFactory 上。路线工作正常,但 autofac 很难找到正确的控制器。
对于那些使用 Autofac 和 MVC 2.0 的用户,Autofac 2.1 不支持区域。如果您的项目足够简单,Autofac 可能似乎可以与区域配合使用,但随着您的区域和控制器变得更加复杂,它就会开始崩溃。
完整区域支持的请求已经关闭,相关代码也已签入,但据我所知,它要到 2.2 才会发布。我将尝试从后备箱进行构建,然后我会报告进展情况。
更新
是的,使用从后备箱构建的 Autofac 一切看起来都很好。希望2.2能够尽快正式发布。
Actually, it turns out the problem is with Autofac 2.1's
AutofacControllerFactory
. The routes are working correctly, but autofac is having a hard time finding the right controller.For those that are using Autofac and MVC 2.0, Autofac 2.1 doesn't have support for areas. If your project is simple enough, Autofac may appear to work with areas, but it will start breaking down as your areas and controllers become more sophisticated.
The ticket for full Area support has been closed and the related code checked in, but from what I can tell it won't be released until 2.2. I'm going to try building from the trunk and I'll report back how it goes.
UPDATE
Yep, everything looks good using Autofac built from the trunk. Hopefully 2.2 will be officially released soon.
您是否尝试过使用带有名称空间参数的重载?
ASP.NET MVC 2 预览版 2:区域重复控制器问题
Have you tried using the overload with the namespaces parameter?
ASP.NET MVC 2 Preview 2: Areas duplicate controller problem