How Routes Work in Web API

Introduction

In this article, I will define how routes work. Here we provide a simple example of a routes application.

Routes

A route can be defined as the sequence of the URL mapped to the handler. The handler can be a physical file or a class . Physical files such as a .aspx file in a web form application and the class that processes the request, in other words the controller in a MVC application. For defining the route in an application, create an instance of the Route class and determine the sequence of the URL and handler.

Let's see the example.

First create an Web API application as in the following:

  • Start Visual Studio 2012.
  • From the start window select "New Project".

  • In the Template Window select "Installed" -> "Visual C#" -> "Web".

  • Select "ASP.NET MVC 4 Web Application" and click on "OK".

    route.jpg

  • From the "MVC4 Project" window select "Web API".

    route1.jpg

Now open the "RouteConfig.cs" file. It exists in "Solution Explorer" -> "App_start" -> "RouteConfig.cs". By default there is only one route that is defined in the "RegisterRoutes" method.

route2.jpg

We will see in this code:

  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 RouteWork  
  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.             );  
  19.         }  
  20.     }  
  21. } 

In the code above, routes defines the "route name" , "URL" and "default parameter". The route name that is defined in the "RegisterRoutes" method can be anything but must be unique and the default route that is already defined maps to "Controller/action/id".

Now open the "HomeController". This exists in the "SolutionExplorer" -> "Controller" -> "HomeController".

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. namespace RouteWork.Controllers  
  7. {  
  8.     public class HomeController : Controller  
  9.     {  
  10.         public ActionResult Index(string ID)  
  11.         {  
  12.             return View();  
  13.         }  
  14. }  
Add an another action method "Item":
  1. public ActionResult Item(int ID)  
  2. {  
  3.     Response.Write("Value of Item ID= " + ID);  
  4.     return View();  
  5. }  

We can add another route such as:

  1. public static void RegisterRoutes(RouteCollection routes)  
  2. {  
  3.     routes.MapRoute(  
  4.                 name:"MyRoute",  
  5.                 url: "{controller}/{action}/{item_no}",  
  6.                 defaults: new { controller = "Home", action = "Item", Item_no = (string)null }  
  7.                 );  
  8. } 

Here we add another route in which the URL "controller/action/item_no" maps to the "Home/Item/2".  The controller is "Home", the action method is "Item" and item_no is a "parameter" that is 2.

In the Index view write this sample of code:

  1. <html>  
  2. <head runat="server">  
  3.     <title>Index</title>  
  4. </head>  
  5. <body>  
  6.     <div>  
  7.         <h2>  
  8.             <a href="Home/Item/2">Click on this link "Item"</a>  
  9.         </h2>  
  10.     </div>  
  11. </body>  
  12. </html>   

And also create an "Item" view. For creating the Item view use this procedure:

  • In the "HomeController".

  • Right-click on the Action method then select "Item" -> "AddView".

    route3.jpg

  • Click on the "Ok" button.

    route4.jpg

When the application is executed it first invokes the "Application_Start" method that exists in the "Global.asax.cs" file. This method in turn calls the RegisterRoute method, this method creates a table for the route. Only one route creates a table. If we create more than one route then it is added to the route table. See this method:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Http;  
  6. using System.Web.Mvc;  
  7. using System.Web.Optimization;  
  8. using System.Web.Routing;  
  9. namespace RouteWork  
  10. {  
  11.     public class WebApiApplication : System.Web.HttpApplication  
  12.     {  
  13.         protected void Application_Start()  
  14.         {  
  15.             AreaRegistration.RegisterAllAreas();  
  16.             RouteConfig.RegisterRoutes(RouteTable.Routes);  
  17.         }  
  18.     }  
  19. }  

Now execute the application, press "F5":

route5.jpg

Click on this link and see the output.

route6.jpg


Similar Articles