Areas in ASP.NET MVC
Areas in ASP.NET MVC
Areas are some of the most important components of ASP.NET MVC projects. The main use of Areas is to physically partition a web project in separate units. In short, areas can be defined as smaller functional units in an ASP.NET MVC project with their own set of controllers, views, and models. Learn more here.
Join the DZone community and get the full member experience.Join For Free
What is an Area in ASP.NET MVC?
Areas are some of the most important components of ASP.NET MVC projects. The main use of Areas is to physically partition a web project in separate units. If you look into an ASP.NET MVC project, logical components like Model, Controller, and the View are kept physically in different folders, and ASP.NET MVC uses naming conventions to create the relationship between these components. Problems start when you have a relatively big application to implement. For instance, if you are implementing an E-Commerce application with multiple business units, such as Checkout, Billing, and Search etc. Each of these units have their own logical components views, controllers, and models. In this scenario, you can use ASP.NET MVC Areas to physically partition the business components in the same project.
In short, an area can be defined as: smaller functional units in an ASP.NET MVC project with its own set of controllers, views, and models.
A single MVC application may have any number of Areas. Some of the characteristics of Areas are:
- An MVC application can have any number of Areas.
- Each Area has its own controllers, models, and views.
- Physically, Areas are put under separate folders.
- Areas are useful in managing big web applications.
- A web application project can also use Areas from different projects.
- Using Areas, multiple developers can work on the same web application project.
Creating Areas in an MVC project is very simple. Simply right click on the project->Add->Area as shown in the image below:
Here you will be asked to provide the Area name. In this example, let’s name the Area “Blogs” and click on Add.
Let us stop for a moment here and explore the project. We will find a folder Areas has been added, and inside the Areas folder, we will find a subfolder with the name Blogs, which is the area we just created. Inside the Blogs subfolder, we will find folders for MVC components Controllers, Views, and Models.
In the Area Blogs folder we will find a class BlogAreaRegistration.cs. In this class, routes for the Blog Area have been registered.
Now, we can add Controllers, Models, and the Views in the Area in the same way we add them normally in an MVC project. For example, to add a controller, let’s right click on the controller folder in the Blogs folder and click on Add->Controller. So let us say we have added a HomeController in the Blogs controller. You will find the added controller in the project as shown in the image below:
You will find that the HomeController has a method called Index. To create a view, right click on the Index action and select Add View as shown in the image below:
On the next screen, we need to select the view template, model class, and others. To keep things simpler, let us leave everything default and click on the Add button to create a View for the Index action in the Home controller of the Blogs Area.
We will see that a view was created inside the Views subfolder of the Blogs folder as shown in the image below:
To verify, let us go ahead and change the title of the view as shown in the image below:
So far we have created:
- An Area with the name Blogs
- A controller inside that, named Home
- A view for the Index action for the Home controller
- Change the title of the view
As a last step to work with Areas, we need to verify whether the Areas are registered in the App_Start of the project or not. To do this, open global.asax and add the highlighted line of code below (if it’s not there already):
Now that we have created the Areas, let us go ahead and run the application and notice the URL.
As highlighted, to invoke the controller of the area, we need to use:
In this case Home controller of Blog area is invoked by:
As you’ve seen here, Areas are some of the most important components of ASP.NET MVC, allowing us to break big projects into smaller, more manageable units, as demonstrated in this blog’s example. I hope you find the post useful, and thanks for reading!
This article was written by Dhananjay Kumar.
Published at DZone with permission of Josh Anderson , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.