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();
- }
- }
- public ActionResult Item(int ID)
- {
- Response.Write("Value of Item ID= " + ID);
- return View();
- }





Join the conversation! Your thoughts help the community grow.