How to do it...

  1. 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(); 
            routeData.Values["controller"] = "Products"; 
            routeData.Values["action"] = "Details"; 
            routeData.DataTokens["price"] = price; 
            context.RouteData = routeData; 
 
            return _innerRoute.RouteAsync(context); 
        } 
} 
  1. Next, let's create an extension method to add this route ...

Get ASP.NET Core MVC 2.0 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.