Posted on 4/3/2020 12:35:57 PM by Admin

Areas in Asp.Net MVC

Areas in MVC are used for modular development. Now, it becomes important to know what exactly modular development is ,and what are the advantages of modular development.

Breaking a large project into smaller modules is known as modular development and it has following advantages.
  1. Faster and parallel development.
  2. Easier to understand, design and test than the larger project.
  3. Ease of debugging and modification, meaning errors can easily be detect and modify.

If we are working on a large project which deals with a lot of functionalities and we have not followed the modular approach then even for a small modification ,we will have to search the whole project ,which will take a lot of our time, similarly addition of new functionality will lead to search the whole project in order to find the correct place to add the functionality.

On the other hand ,if our project is divided into different modules then there will be no need to search the whole project and we can directly add or modify functionality in the corresponding module.

Areas in MVC serves the same purpose and encourages us to follow the modular approach for application development for better readability, maintainability and parallel development.

Suppose, you have a large project and you have successfully divided it into different modules like below.

Modules
Different modules

In MVC each of your module will be represented by a separate Area like shown below.

Areas in mvc
Area

From the above figure it is very clear that after implementing Area each of your module start representing individual Area having its own Controller, Views, Models, WebConfig and Routing section.

So, you do not need to worry about the whole project and you can easily concentrate on one Module at a time ,whereas other developer can work on other modules without disturbing you in parallel fashion.

Implement Area in MVC is very simple and just require adding area in the project folder like explained below.

Right click on the project folder and Add Area ,and give it an appropriate module name and press Add. That's it.

Add Area
Add Area
 Area Name
Area Name

Now let's check the folder structure created by visual studio of a particular Area ,under Areas Folder.

 Folder Structure of Area
Folder structure of Area

As, you can notice folder structure of an Area is very much similar to Folder structure of an MVC template. The only difference is, instead of Global Routing for the whole project the Area contains its own Routing section.The AreaRegistration.cs class has a RegisterArea method where we can define custom routes.

 Defining route for area
define route for area

Defining route is similar to defining route for the whole project in RouteConfig.cs but it has one difference which is very logical and make sense ,that the modular approach has even applied to the routes as well. Meaning as each area actually represents a module of your project ,why not your URL should represent the same. So each of URL starts with the area name signifies that the request is for a particular module only.

So the URL :- http://localhost:1077/Suppliear/Home/Index is self explanatory that it is a get request for index action method of home controller of Supplier module(Area).

However if you do not like the modular implementation of your routes then you can change the route definition and can remove the area name anytime. Though ,I will not recommend it.

If anything is registered it must be called from somewhere .In Asp.Net MVC whether it be your RegisterRoute method of RouteConfig class or RegisterArea of AreaRegistration class ,it is called by the same event that is none other than your Application_Start event in your global.asax file.

 Defining route for area
define route for area