ASP.NET MVC系列:Area
2015-07-14 18:05
681 查看
1. Area简介
ASP.NET MVC Area机制构建项目,可以将相对独立的功能模块切割划分,降低项目的耦合度。2. Area设置Routing
新建Admin Area后,自动创建AdminAreaRegistration.cs,用于设置Area Routing。using System.Web.Mvc; namespace Libing.Portal.Web.Areas.Admin { public class AdminAreaRegistration : AreaRegistration { public override string AreaName { get { return "Admin"; } } public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( name: "Admin_default", url: "Admin/{controller}/{action}/{id}", defaults: new { action = "Index", id = UrlParameter.Optional } ); } } }
在Global.asax.cs的Application_Start(),注册Area路由。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; namespace Libing.Portal.Web { public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); // 注册Area Routing RouteConfig.RegisterRoutes(RouteTable.Routes); } } }
3. 解决默认站点与Area相同Controller名称的Routing设置
当默认站点与Area中有相同名称的Controller时,在Routing中添加namespace。Areas/Admin/AdminAreaRegistration.cs
using System.Web.Mvc; namespace Libing.Portal.Web.Areas.Admin { public class AdminAreaRegistration : AreaRegistration { public override string AreaName { get { return "Admin"; } } public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( name: "Admin_default", url: "Admin/{controller}/{action}/{id}", defaults: new { action = "Index", id = UrlParameter.Optional }, namespaces: new string[] { "Libing.Portal.Web.Areas.Admin.Controllers" } ); } } }
App_Start/RouteConfig.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; using Libing.Portal.Web.Models.Constraints; namespace Libing.Portal.Web { public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, namespaces: new string[] { "Libing.Portal.Web.Controllers" } ); } } }
4. 在默认网站与Area网站的链接
@Html.ActionLink("AdminIndex", "Index", new { controller = "Home", area = "Admin" })
相关文章推荐
- ASP.NET DropDownList控件回传值无刷新操作。
- Asp.Net MVC WebApi2 自动生成帮助文档
- ASP.Net 的幾種畫面 跳轉
- 7 天玩转 ASP.NET MVC - 第 1 天
- Win2008R2 配置 ASP环境,出现500错误的解决方法
- ASP.Net使用母版页窗
- 转-Asp.Net MVC及Web API框架配置会碰到的几个问题及解决方案
- 如何使用EF来进行ASP.net的开发
- 七天学会ASP.NET MVC (六)――线程问题、异常处理、自定义URL
- 七天学会ASP.NET MVC (六)——线程问题、异常处理、自定义URL
- 七天学会ASP.NET MVC (六)——线程问题、异常处理、自定义URL
- asp.net mvc4 简单使用Autofac依赖注入小结
- asp.net 2.0中允许一次上传多个文件的设计[转]
- ASP.Net 获取当前时间,包含各种时间格式
- 【转】ASP.NET导出word和excel通用代码
- Asp.net中用Jmail发送邮件
- asp.net后台调用js方法
- asp.net(c#)上传文件时检测文类型方法小结
- [转]ASP.net页面跳转方式比较
- asp.net文件操作小例子(创建文件夹,读,写,删)【转】