MVC 添加Area

在MVC项目中常常会使用到Area来分开不一样的模块让项目结构更加的清晰。

步骤以下:

项目 –> 添加 -> 区域 ( Area )

输入 Admin

添加成功后

Area包含:前端

建立一个空MVC工程结构相似,Admin Area 有本身的 Controllers、Models 和 Views 文件夹,不同的地方就是多了一个 AdminAreaRegistration.cs 文件,这个文件中定义了一个叫 AdminAreaRegistration 的类,它的内容以下:

根目录能够放一套同样的结构用来作前端开发使用,而admin 目录通常会做为管理员后台来开发!ide

AdminAreaRegistration.cs 文件,这个文件中定义了一个叫 AdminAreaRegistration 的类,它的内容以下:url

namespace MvcApp4.Areas.Admin
  {
      public class AdminAreaRegistration : AreaRegistration
      {
          public override string AreaName
          {
              get
              {
                  return "Admin";
             }
         }
 
        public override void RegisterArea(AreaRegistrationContext context)
         {
            context.MapRoute(
                 "Admin_default",
                 "Admin/{controller}/{action}/{id}",
                 new { controller = "home", action = "Index", id = UrlParameter.Optional },
                 namespaces: new[] { "MvcApp4.Areas.Admin.Controllers" } //指定该路由查找控制器类的命名空间
             );
         }
     }
}

在这里须要注意需加入 Areas 所在的命名空间,来控制 controllers 接收的参数,否则访问会出现错误,往下一点会提到。spa

namespaces: new[] { "MvcApp4.Areas.Admin.Controllers" }

AreaRegistrationContext 类的 MapRoute 方法和 App_Start-> RouteConfig.cs  的 MapRoute 方法的使用是同样的,只是区分Area 目录下的路由控制!

在 Global.asax 中的 Application_Start 方法会自动加了这样一句代码

protected void Application_Start() {
     AreaRegistration.RegisterAllAreas();
 
     WebApiConfig.Register(GlobalConfiguration.Configuration);
     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
     RouteConfig.RegisterRoutes(RouteTable.Routes);
     BundleConfig.RegisterBundles(BundleTable.Bundles);
 }

  

调用 AreaRegistration.RegisterAllAreas 方法让MVC应用程序在启动后会寻找全部继承自 AreaRegistration 的类,并为每一个这样的类调用它们的 RegisterArea 方法。code

下面咱们来作一个 Demoblog

新建两个访问链接,内容都是同样,都是简单输出一个 "hello World"继承

URL定位到 (areas/admin)路由

http://localhost:18291/Admin/Home/Index开发

URL定位到(根目录)get

http://localhost:18291/Home/Index 

public class HomeController : Controller
      {
          //
          // GET: /Admin/Home/
  
          public ActionResult Index()
          {
              return Content("hello world");
          }
 
     }
若是刚才没有加入:

namespaces: new[] { "MvcApp4.Areas.Admin.Controllers" }

运行后就会出现以下错误:

可是若是咱们把根目录下的 /Home/Index 的内容输出改为  “Root Say hello World” , 你会发现仍是输出 “ hello World ”,

这是就是  Controller的歧义问题

这就是咱们须要注意的另外一个地方

咱们须要在App_start下的 RouteConfig.cs 也要增长一个 namespaces 来声明 Controller 访问的命名空间!

//App_start下的 RouteConfig.cs   
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[] { "MvcApp4.Controllers" }//指定该路由查找控制器类的命名空间 controllers
          );
      }
  }
 //areas 下的 \Admin\AdminAreaRegistration.cs 
  public class AdminAreaRegistration : AreaRegistration
  {
      public override string AreaName
      {
          get
          {
              return "Admin";
          }
      }
      public override void RegisterArea(AreaRegistrationContext context)
      {
          context.MapRoute(
              "Admin_default",
              "Admin/{controller}/{action}/{id}",
              new { controller = "home", action = "Index", id = UrlParameter.Optional },
              namespaces: new[] { "MvcApp4.Areas.Admin.Controllers" } //对应的命名空间的 controllers
          );
      }
  }

这样访问时就能够区分 , 不一样目录的 controller

相关文章
相关标签/搜索