I was reading about Request life cycle in MVC. I got stuck in understanding the below line.
The UrlRoutingModule Intercepts the Request
Query - What is UrlRoutingModule?
I searched a lot on google but could not found any useful
I was reading about Request life cycle in MVC. I got stuck in understanding the below line.
The UrlRoutingModule Intercepts the Request
Query - What is UrlRoutingModule?
I searched a lot on google but could not found any useful
Requests to an ASP.NET MVC-based Web application first pass through the UrlRoutingModule
object, which is an HTTP module. This module parses the request and performs route selection. The UrlRoutingModule
object selects the first route object that matches the current request. (A route object is a class that implements RouteBase
, and is typically an instance of the Route
class.) If no routes match, the UrlRoutingModule
object does nothing and lets the request fall back to the regular ASP.NET or IIS request processing.
From the selected Route object, the UrlRoutingModule
object obtains an object that implements the IRouteHandler
interface and that is associated with the Route
object. Typically, in an MVC application, this will be an instance of the MvcRouteHandler
class. The MvcRouteHandler
instance creates an MvcHandler
object that implements the IHttpHandler
interface. The MvcHandler
object then selects the controller that will ultimately handle the request. For more information, see ASP.NET Routing.
The UrlRoutingModule
and MvcRouteHandler
classes are the entry points to the ASP.NET MVC framework. They perform the following actions:
Select the appropriate controller in an MVC Web application.
Obtain a specific controller instance.
Call the controller's Execute
method.
If you look inside the Global.asax of an MVC project, you will see a method called RegisterRoutes
. Within this, you'll see the "default" ASP.NET MVC route:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
This route specifies the format of the URL request and how this should be mapped to controller actions, i.e. the first part of the route identifies the controller, the second part the action, etc. You can add additional custom routes to this, which allows you to work with URLs in different formats.
The UrlRoutingModule
is simply the class that's responsible for taking the incoming request URL and matching it to one of these routes, so the correct controller action is executed.
From MSDN:
The UrlRoutingModule class matches an HTTP request to a route in an ASP.NET application. The module iterates through all the routes in the RouteCollection property and searches for a route that has a URL pattern that matches the format of the HTTP request. When the module finds a matching route, it retrieves the IRouteHandler object for that route. From the route handler, the module gets an IHttpHandler object and uses that as the HTTP handler for the current request.
Just to add few more details.
As mentioned by @Ant P below code is used to register route
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Actually MapRoute
is a Extension method
on Routes collection, and it internally implemented as below.
Note: Its just a pseudo code as per my understanding.
var url = "{controller}/{action}/{id}";
var defaults = new RouteValueDictionary();
defaults.Add("controller", "Home");
defaults.Add("action", "Index");
defaults.Add("id", UrlParameter.Optional);
var routeHandler = new MvcRouteHandler();
var Default = new Route(url,defaults,routeHandler);
routes.Add(Default);
MvcRouteHandler's GetHttpHandler()
method returns an instance of MvcHttpHandler
class, which internally handle further request.
Hope this will help to understand role of UrlRoutingModule
© 2022 - 2024 — McMap. All rights reserved.