ASP.NET MVC3 Areas 分离项目 同名控制器(同名Controller) 演示demo

为什么需要分离?

我们知道MVC项目各部分职责比较清晰,相比较ASP.NET Webform而言,MVC项目的业务逻辑和页面展现较好地分离开来,这样的做法有许多优点,比如可测试,易扩展等等。但是在实际的开发中,随着项目规模的不断扩大,Controller控制器也随之不断增多。如果在Controllers文件夹下面有超过两位数controller,即便采用良好的命名规范,或者用子文件夹的形式区分不同功能的控制器,还是会影响项目的可阅读性和可维护性。因此,在一些场景下,如果能把与某功能相关的文件分离到一个独立的项目中是非常有用的。Asp.Net MVC提供了Areas(区域)的概念达到这一目的。

项目解决方案

前台路由   前台显示页:/new/index   所属控制器:demo.Controllers.NewController

public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute("Default", "{controller}/{action}", new { controller = "Home", action = "Index" }, new string[] { "demo.Controllers" });

}

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();

RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}

后台路由  后台显示页 :/VIVI_HY_ADMIN/new/index  所属控制器:demo.Areas.VIVI_HY_ADMIN.Controllers

public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get { return "VIVI_HY_ADMIN"; }
}

public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute("VIVI_HY_ADMIN", "VIVI_HY_ADMIN/{controller}/{action}", new string[] { "demo.Areas.VIVI_HY_ADMIN.Controllers" });
}
}

项目DEMO

    运行环境:  VS2010  MVC3  演示数据库:ACCRESS(MDB)

原文地址:https://www.cnblogs.com/superfeeling/p/4863497.html