ASP.NET MVC 中的区域,一次只能工作一个
我的文件夹如下所示:
- (root)/Areas/Admin/Views/..
- (root)/Areas/Admin/Controllers/...
(root)/Areas/Admin/Routes.cs
(root)/Areas/Forum/Views/..
- (root)/Areas/Forum/Controllers /...
(root)/Areas/Forum/Routes.cs
公共类路线:AreaRegistration
{ 公共覆盖字符串区域名称 { 获取{返回“管理员”; } }
public override void RegisterArea(AreaRegistrationContext context) { 上下文.MapRoute( “管理员_默认”, "{控制器}/{动作}/{Id}", 新{控制器=“管理员”,操作=“索引”,Id =(字符串)null} ); }
}
公共课程路线:区域注册 { 公共覆盖字符串区域名称 { 获取{返回“论坛”; } }
public override void RegisterArea(AreaRegistrationContext 路由) { 路线.MapRoute( “论坛_默认”, “{控制器}/{操作}”, 新的{控制器=“论坛”,操作=“索引”} ); }
}
起始页
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
AreaRegistration.RegisterAllAreas();
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
应该是 Home/Index 但它以 Admin/Index 开头,为什么
只有 site.com/Admin 有效,site.com/Forum 无效
我应该如何让管理和论坛区域正常工作?为什么只有管理员可以工作而论坛不能工作?
当我删除 Admin/Routes.cs 文件时论坛开始工作...
编辑:
~/Views/ 中的主页不会显示为起始页,即使我
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
在 Global.asax 中位于 AreaRegistration.RegisterAllAreas 之后();
My folder look like this:
- (root)/Areas/Admin/Views/..
- (root)/Areas/Admin/Controllers/...
(root)/Areas/Admin/Routes.cs
(root)/Areas/Forum/Views/..
- (root)/Areas/Forum/Controllers/...
(root)/Areas/Forum/Routes.cs
public class Routes : AreaRegistration
{
public override string AreaName
{
get { return "Admin"; }
}public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Admin_Default", "{controller}/{action}/{Id}", new { controller = "Admin", action = "Index", Id = (string)null } ); }
}
public class Routes : AreaRegistration
{
public override string AreaName
{
get { return "Forum"; }
}public override void RegisterArea(AreaRegistrationContext routes) { routes.MapRoute( "Forum_Default", "{controller}/{action}", new { controller = "Forum", action = "Index"} ); }
}
Global.asax
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
AreaRegistration.RegisterAllAreas();
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
The startpage should be Home/Index but it start with Admin/Index, why?
Only site.com/Admin works not site.com/Forum
How should i get Admin and Forum Areas to work right? Why is only Admin working and not Forum?
When i delete Admin/Routes.cs file Forum start to work...
EDIT:
Home in ~/Views/ don't show as startpage even if i have
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
in my Global.asax after AreaRegistration.RegisterAllAreas();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信你的区域映射应该是这样的结构。
并
防止您的路线发生冲突,这就是我认为您的情况发生的情况。因为您的默认路由与您的管理路由相匹配。
I believe your area mappings should be structured like so.
and
Keeps your routes from conflicting, which is what i think is happening in your case. As your default route matches your admin route.