Creating a custom route using IRouter
In this recipe, we will learn how to create a custom route.
Getting ready
In MVC 6, we will use IRouter instead of IRouteHandler and IHttpHandler to define routes. ASP.NET Core works with recursive implementations of IRouter. At higher level, it can write an HTTP response, and at lower level it add my routing table to the routing middleware.
How to do it...
- First, the class we create has to implement IRouter and the
RouteAsync
method:
public class ProductsRouter : IRouter { private readonly Route _innerRoute; public VirtualPathData GetVirtualPath (VirtualPathContext context) { return null; } public Task RouteAsync(RouteContext context) { // Test QueryStrings var qs = context.HttpContext.Request.Query; var price = qs["price"]; if(string.IsNullOrEmpty(price)) { return Task.FromResult(0); } var routeData = new RouteData(); ...