Quicktostudy
Contact Us| About Us| Terms| Privacy Policies
Dot Net MVC Tutorial

Area in MVC

Area in MVC

Why we use area in mvc

As we know MVC Architecture separate folders for Model, View and Controller. it provides logical separation and physically separation also. For large application it difficult to manage. So area allows us to partition large application into smaller units where each unit contains separate MVC folder structure, same as default MVC folder structure.

For example : Large enterprise application may have different modules like admin, finance, HR, marketing etc. So an Area can contain separate MVC folder structure for all these modules as shown below. Area was introduced with MVC 2 release.

Area in MVC

How to create area in MVC

You can create an Area in MVC 4 and Visual Studio 2012 for web by right clicking on the project in the solution explorer -> Add -> Area

Area in MVC

You can create an Area in MVC 5 and Visual Studio 2013 for web by right clicking on the project in the solution explorer -> Add -> Scaffold

Area in MVC

Enter Area name in Add Area dialogue box and click Add. Below is the folder structure for area in mvc

Area in MVC

As we can see, each area includes AreaRegistration class in {area name} + AreaRegistration.cs file. (Example : AdminAreaRegistration.cs)
The following is AdminAreaRegistration class created with admin area.

AdminAreaRegistration.cs : Area Registration

  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 { action = "Index", id = UrlParameter.Optional }
          );
      }
  }
  

AreaRegistration class overrides RegisterArea method to map the routes for the area.

All the area must be registered in Application_Start method in Global.asax.cs as AreaRegistration.RegisterAllAreas();

  protected void Application_Start()
  {
      AreaRegistration.RegisterAllAreas();
      FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
      RouteConfig.RegisterRoutes(RouteTable.Routes);
      BundleConfig.RegisterBundles(BundleTable.Bundles);
  }
  
Contact Us| About Us| Terms| Privacy Policies
Powered by Lorquins Technologies© 2017 QuickToStudy.com. All Rights Reserved