[ASP.NET Core MVC Pipeline] Routing Middleware — Custom IRouter

Middleware Pipeline - Routing
Middleware Pipeline

Why would I do that?

The Basics

namespace Microsoft.AspNetCore.Routing
{
public interface IRouter
{
VirtualPathData GetVirtualPath(VirtualPathContext context);
Task RouteAsync(RouteContext context);
}
}
public class CustomRouter : IRouter
{
private IRouter _defaultRouter;

public CustomRouter(IRouter defaultRouter)
{
_defaultRouter = defaultRouter;
}

public VirtualPathData GetVirtualPath(VirtualPathContext context)
{
return _defaultRouter.GetVirtualPath(context);
}

public Task RouteAsync(RouteContext context)
{
throw new NotImplementedException();
}
}

Routing our Requests

public class CustomRouter : IRouter
{
private IRouter _defaultRouter;

public CustomRouter(IRouter defaultRouteHandler)
{
_defaultRouter = defaultRouteHandler;
}

public VirtualPathData GetVirtualPath(VirtualPathContext context)
{
return _defaultRouter.GetVirtualPath(context);
}

public async Task RouteAsync(RouteContext context)
{
var headers = context.HttpContext.Request.Headers;
var path = context.HttpContext.Request.Path.Value.Split('/');

// Look for the User-Agent Header and Check if the Request comes from a Mobile
if (headers.ContainsKey("User-Agent") &&
headers["User-Agent"].ToString().Contains("Mobile"))
{
var action = "Index";
var controller = "";
if (path.Length > 1)
{
controller = path[1];
if (path.Length > 2)
action = path[2];
}

context.RouteData.Values["controller"] = $"Mobile{controller}";
context.RouteData.Values["action"] = action;

await _defaultRouter.RouteAsync(context);
}
}
}

Hooking up to the Routing Middleware

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseStaticFiles();

app.UseMvc(routes =>
{
routes.Routes.Add(new CustomRouter(routes.DefaultHandler));

routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}

--

--

--

Software Development and everything that encircles it :)

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

500 Subscribers Milestone, and a Similarity

*🌍TRON THUNDER🌏* *🙏 🙏* *EARN DAILY 100 TO 1000 TRX?🏻💫*

How search engine works- Overview

NFTrade and Dragon Evolution Announce Strategic Partnership

6 Questions to Ask Yourself Before Creating a Dashboard

Experience Unfolded: PyCon India 2019

How Can We Remove Barriers to Widespread Adoption of Open Source Infrastructure for Private Clouds?

Ready for Implementation, Done for Deployment

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Lucas Araujo | Azure Coder

Lucas Araujo | Azure Coder

Software Development and everything that encircles it :)

More from Medium

Drools Integration: Moving away from JS engine

Top features and updates in Angular 13 — The Complete Note

Simplifying Third-Party Logistics (3PL) Integrations With EDI & API Integration

Simplifying Third-Party Logistics

The Difference between _() and {% trans %} in Django Templates