This video provides an overview of the URL routing module, and describes the process for route selection and retrieval of the route and HTTP handlers.
- [Instructor] When a request is made to an ASP.net MVC application, it first passes through the URL routing module, which is an HTTP module. The request gets analyzed and parsed within the module, in order to find a matching route. The module looks for a route that has a URL pattern that matches the format of the request. It selects the first route that matches and retrieves the corresponding route handler.
From the route handler, the module can then get the HTTP handler and execute the request. If there are no matching routes, the module lets the request fall back to ASP.net request processing. Let's get back to Visual Studio to take a closer look at how route selection works. Open up the RouxAcademy project and the global.asax file. When the first request comes in to the application, the application start event handler gets called.
Notice the call to the register routes method of the RouteConfig class. It's getting past a collection of routes from the route table. It's important to know that every ASP.net MVC application has a route table. It's purpose is to store the URL routes that are defined, as well as their mappings to a certain controller and action. Each route object in this routes collection inherits from route base.
Now let's go to the definition of RegisterRoutes, which is in the RouteConfig class. You can see that there's a call to the MapRoute method, this is how we define a URL route in the route table, and map it to a controller and action. This is the default route, so it's named Default. And the URL pattern gets defined in the second parameter. The first segment of the URL is mapped to the controller, the second is to the action, and the third segment is to a parameter named ID.
There's also default values for all three parameters, which map to the home controller's index action. You can add your own custom routes and map URL patterns here, once this call to register route's complete, the route table for our application is created. When the routing module intercepts a request, it'll iterate through this collection of routes in the route table until it finds the first matched based on the URL pattern.
Let's look deeper into the inner workings of the URL routing module class. It exposes a method called PostResolveRequestCache. This is one of the HTTP application request lifecycle events we reviewed earlier in the course, and the URL routing module subscribes to it. The logic to match a request to the correct route is in this method. The module gets the right route data object based on the matched route.
Route data has a route handler property which retrieves an object that implements the IRouteHandler interface. IRouteHandler defines a GetHttpHandler method, which provides the IHttpHandler object that will process the current request.
Released
5/1/2018- Fundamentals of the request life cycle
- Designing and implementing HTTP modules and handlers
- Choosing between handlers and modules
- Understanding route handlers
- Configuring convention-based routing
- ASP.NET MVC attribute routing
- Creating a custom controller factory
- Defining a custom dependency resolver
- Creating a custom view engine
Share this video
Embed this video
Video: URL routing module overview