Using Areas in Asp.Net MVC with Example

  Total Views : 1983
  Comments : 0
 
  Keywords : What are the areas in asp.net mvc, How to use areas in asp.net mvc application, Getting started with Areas in Asp.Net MVC

Here we will learn areas in asp.net mvc and how to use areas in asp.net mvc with 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 small web application then the default folder structure work fine to maintain application. But if your application becomes big then it create problem to maintain for example if we have large hospital portal then there are various section in it and if we use default folder structure which we have then 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 simple application

Create New Applicaiton in Asp.Net MVC

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

 

create new asp.net mvc project from visual studio 2012

 

After that you will see 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 name as “AreasDemo” then finally click on OK button.

 

create new asp.net mvc areas application

 

Now new dialog will pop up for selecting 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 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 MVC application, right-click on the project item with in 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 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 above folder structure we have class ChildSectionAreaRegistration.cs  which is created inside ChildSection Area.

Registering Areas in MVC

We need to inform MVC framework that we have added Area this class is auto generated when we add Area we do not required 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 method (AreaName, RegisterArea) as abstract which we are overriding in this ChildSectionAreaRegistration Class. The first method AreaName contains 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 Application_Start event handler by adding 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 application. Once we add 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 registering Area now let's add Controller and View in this Area and check how its works.

Adding Controller in Asp.Net MVC

For adding controller Just 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 name Add Controller in that give name to controller and select template as "Empty MVC Controller" and click on Add button.

Give name to controller in asp.net mvc areas application

After adding 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

For Adding View just right click inside Index ActionResult Method and Select "Add View" to create the view template for our Index form. Now new pop up "Add View" will open in that give view name as "Index" and click Add button (Here we are not changing any option in 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

For accessing URL we need to enter Area Name first then 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 link with area in our view. We need to write action link with area after controller name just 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

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User