MVC中的Areas域

第一步:右键单击 添加 Area 写入一个名字,创建即可。

会自动 创建 该域,目录如下:

 

第二步:在域 和 Global.asax中的 路由加上 命名空间如下:

 new string[] { "Web.Controllers" }

routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } ,// Parameter defaults new string[] { "Web.Controllers" } ); context.MapRoute( "Admin_Default", "Admin/{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new string[] { "Web.Areas.Admin.Controllers" } );

 第三步:域中重新路由,可以写在Global.asax中,一般放在域中如下:

public class AdminArea : AreaRegistration { public override string AreaName { get { return "Admin"; } } public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Admin_Default", "Admin/{controller}/{action}/{id}", new { controller = "User", action = "Index", id = "" }, new[] { "BlogSite.Areas.Admin.Controllers" } ); } }

第四步:在Global.asax中进行注册

protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterRoutes(RouteTable.Routes); }

你可能感兴趣的:(MVC)