What is Routing in ASP .NET MVC?


A Routing is characterized by a few attributes, such as name, URL pattern, default values, constraints, data tokens, and a route handler. The attributes you set most often are name, URL pattern, and default values.

URL routing HTTP module

The URL routing HTTP module processes incoming requests by looking at the URLs and dispatching them to the most appropriate executor. The URL routing HTTP module supersedes the URL rewriting feature of older versions of ASP.NET.

Structure of the URL Routing Module:

The URL routing engine is an HTTP module that wires up the [csharp]PostResolveRequestCache[/csharp] event. The event fires right after checking that no response for the request is available in the ASP.NET cache.

What is Routing in ASP .NET MVC
The HTTP module matches the requested URL to one of the user-defined URL routes and sets the HTTP context to use the ASP.NET MVC standard HTTP handler to serve the request. As a developer, you’re not likely to deal with the URL routing module directly. The module is provided by the system and you don’t need to perform any specific form of configuration. Instead, you are responsible for providing the routes that your application supports and that the routing module will consume.

Application routes:

By design, an ASP.NET MVC application is not forced to depend on physical pages. In ASP.NET MVC, users place requests for acting on resources. The framework, however, doesn’t mandate the syntax for describing resources and actions. I’m aware that the expression “acting on resources” will likely make you think of Representational State Transfer (REST).

Although you can use a pure REST approach within an ASP.NET MVC application, I would rather say that ASP.NET MVC is loosely REST-oriented in that it does acknowledge concepts such as resource and action, but it leaves you free to use your syntax to express and implement resources and actions. As an example, in a pure REST solution, you would use HTTP verbs to express actions—GET, POST, PUT, and DELETE—and the URL to identify the resource.

URL patterns and routes:

A route is a pattern-matching string that represents the absolute path of a URL—namely, the URL string without protocol, server, and port information. A route might be a constant string, but it will more likely contain a few placeholders. Here’s a sample route:

The route is a constant string and is matched only by URLs whose absolute path is /home/test

Route handler:

The route defines a bare minimum set of rules, according to which the routing module decides whether the incoming request URL is acceptable to the application. The component that ultimately decides how to remap the requested URL is another one entirely. Precisely, it is the route handler. The route handler is the object that processes any requests that match a given route. Its sole purpose in life is returning the HTTP handler that will serve any matching request.

Technically speaking, a route handler is a class that implements the IRouteHandler interface. The interface is defined as shown here:

public interface IRouteHandler
IHttpHandler GetHttpHandler(RequestContext requestContext);
public class RequestContext
public RequestContext(HttpContextBase httpContext, RouteData routeData);
public HttpContextBase HttpContext { get; set; }
public RouteData RouteData { get; set; }

Attribute routing: A popular NuGet package included in ASP.NET MVC is Attribute Routing. It is all about defining routes directly on controller actions by using attributes. This routing is based on the conventions established in [csharp]global. asax[/csharp], at the startup of the application.

public ActionResult GetOrderById(int orderId)