Asp.net MVC 3使用 Area 小结
2011-10-12 10:25
489 查看
原文发表在:http://www.birchlee.com/post/2011/10/12/15.aspx
项目分为三个首页
如: /Home/Index 前台首页
/Admin/Home/Index 后台首页
/OA/Home/Index 办公平台首页
新建一个asp.net MVC3 示例项目: 右键 →添加→Area
直接运行项目:
原因是存在同名的多个Controller,需要配置默认的命名空间。解决方法:
打开Global.asax.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },// Parameter defaults
new[] { "Web.Controllers" }// Namespaces 引入默认的命名空间
);
}
http://localhost:49849/ 运行后输出 Home/Index
http://localhost:49849/Admin/Home/Index 运行后输出 Admin/Home/Index
http://localhost:49849/OA/Home/Index 运行后输出 OA/Home/Index
更改路径:
http://localhost:49849/Admin/后报404错误
原因是 Area下面的Admin没有配置默认的 Controller造成的,解决方法:
打开 Area下Admin下 AdminAreaRegistration.cs
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
加上默认的Controller即可。
在此抛砖引玉了。
项目分为三个首页
如: /Home/Index 前台首页
/Admin/Home/Index 后台首页
/OA/Home/Index 办公平台首页
新建一个asp.net MVC3 示例项目: 右键 →添加→Area
直接运行项目:
原因是存在同名的多个Controller,需要配置默认的命名空间。解决方法:
打开Global.asax.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },// Parameter defaults
new[] { "Web.Controllers" }// Namespaces 引入默认的命名空间
);
}
http://localhost:49849/ 运行后输出 Home/Index
http://localhost:49849/Admin/Home/Index 运行后输出 Admin/Home/Index
http://localhost:49849/OA/Home/Index 运行后输出 OA/Home/Index
更改路径:
http://localhost:49849/Admin/后报404错误
原因是 Area下面的Admin没有配置默认的 Controller造成的,解决方法:
打开 Area下Admin下 AdminAreaRegistration.cs
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
加上默认的Controller即可。
在此抛砖引玉了。
相关文章推荐
- [转]Asp.net MVC 3使用 Area 小结
- 【ASP.NET MVC 学习笔记】- 09 Area的使用
- ASP.NET MVC 使用二级域名来注册Area区域
- asp.net mvc view使用小结(TEXTBOX为例)
- 使用asp.net mvc里面的area让网站更有条理
- ASP.NET MVC - Area的路由映射(使用NampSpace)
- ASP.NET MVC Area使用-将Area设置成独立项目
- ASP.NET MVC - Area的路由映射(使用NampSpace)
- asp.net mvc Area使用
- 在ASP.NET MVC中使用Area
- ASP.NET MVC- 在Area里使用RedirectToAction跳转出错的解决方法
- asp.net mvc view 使用小结2
- [ASP.NET MVC 小牛之路]08 - Area 使用
- [ASP.NET MVC 小牛之路]08 - Area 使用
- 创建一个区域(Creating an Area) |使用区域 | 高级路由特性 | 精通ASP-NET-MVC-5-弗瑞曼
- ASP.NET MVC Area使用-将Area设置成独立项目
- [ASP.NET MVC 小牛之路]08 - Area 使用
- ASP.NET MVC Area使用-将Area设置成独立项目
- ASP.NET MVC 下拉列表使用小结
- 填充区域 (Populating an Area) | 使用区域 | 高级路由特性 | 精通ASP-NET-MVC-5-弗瑞曼