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".
- From the "MVC4 Project" window select "Web API".
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.
We will see in this code:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.Web.Routing;
- namespace RouteWork
- {
- public class RouteConfig
- {
- public static void RegisterRoutes(RouteCollection routes)
- {
- routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
- routes.MapRoute(
- name: "Default",
- url: "{controller}/{action}/{id}",
- defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
- );
- }
- }
- }
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".
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace RouteWork.Controllers
- {
- public class HomeController : Controller
- {
- public ActionResult Index(string ID)
- {
- return View();
- }
- }
Add an another action method "Item":
- public ActionResult Item(int ID)
- {
- Response.Write("Value of Item ID= " + ID);
- return View();
- }
We can add another route such as:
- public static void RegisterRoutes(RouteCollection routes)
- {
- routes.MapRoute(
- name:"MyRoute",
- url: "{controller}/{action}/{item_no}",
- defaults: new { controller = "Home", action = "Item", Item_no = (string)null }
- );
- }
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:
- <html>
- <head runat="server">
- <title>Index</title>
- </head>
- <body>
- <div>
- <h2>
- <a href="Home/Item/2">Click on this link "Item"</a>
- </h2>
- </div>
- </body>
- </html>
And also create an "Item" view. For creating the Item view use this procedure:
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:
- 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 RouteWork
- {
- public class WebApiApplication : System.Web.HttpApplication
- {
- protected void Application_Start()
- {
- AreaRegistration.RegisterAllAreas();
- RouteConfig.RegisterRoutes(RouteTable.Routes);
- }
- }
- }
Now execute the application, press "F5":
Click on this link and see the output.