ASP.NET MVC 2 区域,奇怪的路由行为
我创建了一个名为“Admin”的区域。我还在这个区域创建了一个控制器(页面)和一个视图(列表)。
当我运行我的应用程序并输入 url“/Admin/Pages/List”时,我收到“找不到资源”错误。
当我进入 /Pages/List 时,Action 方法被命中,但找不到视图,因为应用程序正在错误的目录中搜索
~/Views/Pages/List.aspx 〜/视图/页面/List.ascx 〜/视图/共享/List.aspx ~/Views/Shared/List.ascx
视图位于 /Admin/Pages/List 中。
我的管理区域路由配置:
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Admin";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller= "Pages",action = "Index", id = "" }
);
}
}
I've created an Area named "Admin". I've created also a controller(Pages) and a view(List) in this areas.
When I run my app and enter the url "/Admin/Pages/List" I'm getting an The resource cannot be found error.
When I enter /Pages/List, the Action method is hit but the view is not found,because the app is searching in wrong directories
~/Views/Pages/List.aspx
~/Views/Pages/List.ascx
~/Views/Shared/List.aspx
~/Views/Shared/List.ascx
the view is in /Admin/Pages/List.
My routing conf for Admin area:
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Admin";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller= "Pages",action = "Index", id = "" }
);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否已将
AreaRegistration.RegisterAllAreas();
添加到 Global.asax.cs这应该在现有的
routes.MapRoute
调用编辑:
之前 运行查看我的管理区域,路由如下所示:
Have you added
AreaRegistration.RegisterAllAreas();
to the Global.asax.csThis should run before your existing
routes.MapRoute
callsEdit:
Just looked at my Admin Area and the routing looks like this: