【发布时间】:2011-10-15 23:17:23
【问题描述】:
也许我没有正确理解 MVC 区域是如何工作的,但这让我有点困惑。
- 在 MVC3 项目的 Visual Studio 中右键单击“添加区域”,添加一个名为“MyArea”的区域
- 为 MyArea 创建一个控制器:“AnArea”,在 MyArea 区域中具有匹配的视图。
- 在 MyAreaAreaRegistration.RegisterArea 方法中将“controller = “AnArea”添加到 context.MapRoute 的默认参数。
因此,此时如果您启动应用程序并导航到 /MyArea/,它应该会加载带有匹配视图的 AnArea 控制器。如果您导航到 /MyArea/AnArea,它将显示相同的结果。
但是,如果您导航到 /AnArea/,仍会找到控制器并显示以下错误消息:
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/anarea/Index.aspx
~/Views/anarea/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/anarea/Index.cshtml
~/Views/anarea/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
这是正确的行为吗?我原以为一个区域的控制器只能通过它自己的区域而不是全局访问。
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-routing asp.net-mvc-areas