ASP.NET MVC - What is UrlRoutingModule?
Asked Answered
P

3

17

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

Preterit answered 20/5, 2013 at 5:15 Comment(0)
C
28

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.

Reference

Cerargyrite answered 20/5, 2013 at 7:38 Comment(3)
Is there any difference between UrlRoutingModule and Http Module ?Preterit
@Cerargyrite : As you said in you first sentence Requests to an ASP.NET MVC-based Web application first pass through the UrlRoutingModule object, which is an HTTP module. Is there any difference between UrlRoutingModule and Http Module ?Hollyanne
An HTTP module is a generic concept of some code what is executed by IIS when is processing requests. This is a very simple description but more doesn't fit into a comment, there is lots of reading on this topic. In .Net is a class what implements IHttpModule interface. System.Web.Routing.UrlRoutingModule is a concrete class what implements IHttpModule and this makes it a concrete HTTP module what does something concrete. There are many HTTP modules in ASP.Net and anyone can create their own.Elide
L
3

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.

Liquidate answered 20/5, 2013 at 7:35 Comment(0)
T
0

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

Typewriter answered 23/2, 2014 at 7:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.