Adding Areas in ASP.Net Web API

Introduction

This article explains Areas in the ASP. NET Web API. Areas are used for the management of the project. They are used in large projects. We create more than one area in a project. In this article, you will see the creation of areas.

Create the Web API application first

  • Start Visual Studio 2012 and select "New Project" from the Start Page.
  • In the Template window, select "Installed template" -> "Visual C#" -> "Web".
  • Choose "ASP. NET MVC 4 Web Application" then change its name.
  • Click on the "OK" button.

    area.jpg
  • From the MVC4 Project Window select Web API.

Add the "Area" to the project

  • In the "Solution Explorer".
  • Right-click on the Project.
  • Select "Add"->"Area".

    area4.jpg
  • Name the area "Collect".

    area3.jpg
  • Click on the "OK" button.

Here add a file "CollectAreaRegistration.cs". Its code looks as in the following:

  1. using System.Web.Mvc;  
  2. namespace MvcApplication1.Areas.Collect  
  3. {  
  4.     public class CollectAreaRegistration : AreaRegistration  
  5.     {  
  6.         public override string AreaName  
  7.         {  
  8.             get  
  9.             {  
  10.                 return "Collect";  
  11.             }  
  12.         }  
  13.         public override void RegisterArea(AreaRegistrationContext context)  
  14.         {  
  15.             context.MapRoute(  
  16.                 "Collect_default",  
  17.                 "Collect/{controller}/{action}/{id}",  
  18.                 new { action = "Index", id = UrlParameter.Optional }  
  19.             );  
  20.         }  
  21.     }  
  22. } 

There is a class "CollectAreaRegistration" and this class is inherited from the "AreaRegistration" class.

Here is an "AreaName" that returns the name of the area that was provided when the Area was added.

In the code above the "RegisterArea" method invokes the "Maproute" method for providing the new route to the Area "Collect".

Create a Controller

  • In the "Solution Explorer".

  • Select the "Area" folder then select "Controller".

  • Right-click on the controller then select "Add" -> "Controller".

  • Open a window, change the name to "HomeController" and Select "MVC controller with empty read/write actions" from the template.

  • Click on the "OK" button.

    area6.jpg

Create a "index.cshtml" file.

  • Open the file HomeController.

  • Right-click on the "Index" Method.

    area8.jpg

  • Click on "Add View ".

    area9.jpg

  • Without performing any change click on the "OK" button.

Now write this code in the "index.cshtml" file.

  1. @{  
  2.     ViewBag.Title = "Index";  
  3. }  
  4. <h2>Areas\Home\Index</h2>  
  5. @{  
  6. Layout = null;  
  7. }  
  8. <!DOCTYPE html>  
  9. <html>  
  10. <head>  
  11. <meta name="viewport" content="width=device-width" />  
  12. <title>Collect Area</title>  
  13. </head>  
  14. <body>  
  15. <h1>Collect Area</h1>  
  16. <div>  
  17.   @*@RenderBody()*@  
  18. </div>  
  19. </body>  
  20. </html> 

Perform some changes in the RouteConfig.cs file.

This file exists in "App_Start" -> "RouteConfig.cs", we add a namspace that is called the Maproute method that provides the new route to the Area. We pass the namespace "MVCApplication1.Controller" as a parameter. The entire code for the RouteConfig.cs file looks like this:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using System.Web.Routing;  
  7. namespace MvcApplication1  
  8. {  
  9.     public class RouteConfig  
  10.     {  
  11.         public static void RegisterRoutes(RouteCollection routes)  
  12.         {  
  13.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  14.             routes.MapRoute(  
  15.                 name: "Default",  
  16.                 url: "{controller}/{action}/{id}",  
  17.                 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }  
  18.                  namespaces: new[] { "MvcApplication1.Controllers" }  
  19.             );  
  20.         }  
  21.     }  
  22. } 

To execute the application, press F5.

area1.jpg

Change the URL to something like http://localhost:11914/Collect/Home. The output looks as in the following:

area2.jpg