Using Areas in Asp.Net MVC with Example

Here we will learn areas in asp.net mvc and how to use areas in asp.net mvc with a simple example. In asp.net mvc, when we partition web applications into smaller units that are referred as areas. Areas provide a way to separate a large mvc web application into smaller functional groupings.

 

If we have a small web application, then the default folder structure works fine to maintain the application. But if your application becomes big, it screates a problem to maintain for example, if we have a large hospital portal, then there are various sections in it, and if we use a default folder structure, it will create a mess. In this condition, we need to use Areas for separating our application. Now we will learn areas in asp.net with a simple application..

Create New Application in Asp.Net MVC

Now let’s create a basic ASP.NET MVC 4 application to understand areas in asp.net for that Open visual studio à Go to File à Select New à Select Project.

 

create new asp.net mvc project from visual studio 2012

 

After that, you will see a new dialog will pop up for selecting your Template and Project type. From Templates, select Visual C# à inside that select Web and then project type select ASP.NET MVC 4 Web Application, and here we are giving the name as “AreasDemo” finally click on OK button.

 

create new asp.net mvc areas application

 

Now a new dialog will pop up for selecting a template in that Select Basic Template and click ok it like as shown below.

 

select template type basic for asp.net mvc areas project

 

After clicking on the OK button, a simple asp.net mvc project will be created that will be like as shown below.

 

Areas project structure in asp.net mvc

Creating Area in Asp.Net MVC Application

To add an Area to the MVC application, right-click on the project item within the Solution Explorer window and select Add à Area.

 

Adding new area in asp.net mvc application

 

 After clicking on Area, a New wizard will popup for Asking Area Name in that give name as "ChildSection" like as shown below.

 

give name to area in asp.net mvc application

 

Now click on the Add button the Area with Name ChildSection will create like as shown below.

 

after adding area section in asp.net mvc application

 

After adding, if we check our folder structure, we have an Areas folder created inside that we have another folder which contains Area with name ChildSection and all basic folder structure (Models/Views/Controllers) with A Web.config file under the Views folder. This contains the necessary entries for the RazorViewEngine to function properly.

 

areas folder sections in asp.net mvc application

 

If we check the above folder structure, we have class ChildSectionAreaRegistration.cs that is created inside ChildSection Area.

Registering Areas in MVC

We need to inform the MVC framework that we have added Area. This class is auto-generated when we add Area, we do not require to write it explicitly.

 

using System.Web.Mvc;

namespace AreasDemo.Areas.ChildSection

{

public class ChildSectionAreaRegistration : AreaRegistration

{

public override string AreaName

{

get

{

return"ChildSection";

}

}

public override void RegisterArea(AreaRegistrationContext context)

{

context.MapRoute(

"ChildSection_default",

"ChildSection/{controller}/{action}/{id}",

new { action = "Index", id = UrlParameter.Optional }

);

}

}

}

This ChildSectionAreaRegistration class is inheriting from Abstract class AreaRegistration, which has two methods (AreaName, RegisterArea) as abstract, which we are overriding in this ChildSectionAreaRegistration Class. The first method AreaName, contains the name of your Area ( ChildSection ) and the second method contains Routing for ChildSection Area.

Registering Areas in Global.asax

In Global.asax, we need to register all Areas in the Application_Start event handler by adding the below code.

 

AreaRegistration.RegisterAllAreas();

Here RegisterAllAreas() Method is a static method. and this RegisterAllAreas() method will call all RegisterArea() methods of all Areas found in the application. Once we add the above code in Global.asax Application_Start event that will be like as shown below.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Http;

using System.Web.Mvc;

using System.Web.Optimization;

using System.Web.Routing;

 

namespace AreasDemo

{

// Note: For instructions on enabling IIS6 or IIS7 classic mode,

// visit http://go.microsoft.com/?LinkId=9394801

public class MvcApplication : System.Web.HttpApplication

{

protected void Application_Start()

{

AreaRegistration.RegisterAllAreas();

WebApiConfig.Register(GlobalConfiguration.Configuration);

FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

RouteConfig.RegisterRoutes(RouteTable.Routes);

BundleConfig.RegisterBundles(BundleTable.Bundles);

}

}

}

After completing the registering Area now, let's add Controller and View in this Area and check how it works.

Adding Controller in Asp.Net MVC

To add controller, Right click on Controller folder inside that select Add and then select Controller.

 

Add controller in asp.net mvc areas application

 

After clicking on controller, new dialog will popup with the name Add Controller in that give the name to the controller and select template as "Empty MVC Controller" and click on the Add button.

Give name to controller in asp.net mvc areas application

After adding a new controller, our project will be like as shown below.

 

After adding new controller in asp.net mvc application

 

Our controller will contain code like as shown below.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

 

namespace AreasDemo.Areas.ChildSection.Controllers

{

public class ChildRegistrationController : Controller

{

//

// GET: /ChildSection/ChildRegistration/

public ActionResult Index()

{

return View();

}

}

}

Adding View in Asp.Net MVC Application

Tp add View, right-click inside Index ActionResult Method and Select "Add View" to create the view template for our Index form. Now new popup "Add View" will open in that give the view name as "Index" and click the Add button (Here we are not changing any option in the wizard) like as shown.

 

Adding view to controller by right click in asp.net mvc application

 

After adding view, our project structure will be like as shown below.

 

After adding view in asp.net mvc application our project structure

 

Now run your application and see the output.

Accessing URL in Asp.Net MVC

To access URL, you need to enter the Area Name first, then the Controller Name, and lastly Action name.

 

  • Area Name - ChildSection.
  • Controller Name - ChildRegistration
  • Action Name - Index

Our URL will be like as http://localhost:4099/ChildSection/ChildRegistration/Index, and our output will be like as shown below.

 

Output of areas in asp.net mvc application exampleHere we will see how to create links with areas in our view. We need to write an action link with the area after controller name add New { area= “Area Name”} like as shown below.

 

@Html.ActionLink("ChildRegistration Details", "Index", "ChildRegistration", new { area = "ChildSection" }, null)

Our output will be like as shown below.

 

after adding areas link in asp.net mvc application