Let’s move on with our series about the Core MVC Pipeline. What is the next step? Last time we learned how to point a request to a specific Router, and what if we need to handle a route in a custom way? Let’s create our custom Route Handler.
As we can see, the Route Handler is the last step inside our Routing Middleware and, as long as the request matches any of the routes, is also the last step inside the Middleware Pipeline, we are almost ready to move on to the next phase on the Core MVC Pipeline.
But what exactly does the Route Handler do? Let’s think functionally:
- Our Web Server received a request and, since it is a valid request, passed it to our application.
- Our application passed the request to our Middleware pipeline which running the request through every middleware until one of them decides to end the request.
- The request went through a lot of processes and reached our Routing Middleware.
- Our routing middleware will pass the request to our Router who will see if this request matches any known route. If it doesn’t, the middleware will pass the request to the next middleware, or initiate the response.
But what if the request matches any known route? What is next? That is where our Route Handler gets to do its magic!
The Route Handler is the component that defines what to do with the Request that matches a Route on our system. The default implementation for this component is the MvcRouteHandler, which usually handle every single request accepted by the Router, but now we are going to implement our own Route Handler.
This component suffered a great deal of changes in the way it is structured on the Full .NET Framework in comparison with the .NET Core implementation.
To implement our Route Handler we are simply going to need a RequestDelegate. This request delegate will contain all the logic that must be executed against a matched route and create a response.
The code for this example will be very simple, as this is a very specialized component, and I don’t see it getting overriding frequently.
private static RequestDelegate CustomRouteHandler()
return async (c) =>
await Task.Run(() => c.Response.Redirect("http://azurecoder.net"));
It is this simple! In this example any request matched and sent to our RouteHandler will redirect the user to this Blog 🙂 The object “c” is a HttpContext object, so anything that can be done with a request/response flow can be done in the RouteHandler as well.
Now we only have to hook up our RouteHandler on the Routing Middleware:
// Main Route
In the case (line 3) we created a Route that will match ANY request in which the URI starts with “blog/” and send it to our custom router, which will redirect the request.
And that is all there is for a simple custom router 🙂
There is of course other ways to implement Route Handlers but this should be more than enough for the very specific cases where this is necessary.
This wraps up our talk about the Middleware part of the MVC Pipeline! In the next post we start the talk on the Controller Initialization!
See you next time 🙂
Also published on Medium.