ASP .NET MVC has provide more flexible options to extend the functionality to make a programmer easy and smooth.ย Custom route handlers are one of them.
How MVC Routing Works?
URLs are very important part of our web based application. Routing will decideย which handler will be responsible to handle the incoming request from the browser and responsibleย for match the incoming URL with route table to proceed. Let we understand more on routing ย here.
Why and Where We Need a Custom Route Handler?
Scenario 1: If we decide to process some data before creating the instance of the controller.
Scenario 2: It will helpful when we need to redirect the user to any external page or shortened the long URLs or making the URLs more user-friendly.
What is Route Handlers ?
From MSDN โย ”ย MvcRouteHandler. This class implements IRouteHandler, therefore it can integrate with ASP.NET routing. The MvcRouteHandler class associates the route with an MvcHandler instance. A MvcRouteHandler instance is registered with routing when you use the MapRoutemethod. When the MvcRouteHandler class is invoked, the class generates an MvcHandler instance using the current RequestContextinstance. It then delegates control to the new MvcHandler instance.โ
In simple, it is a class that implements IRouteHandler interface, which provides route handler class instance to process the request.ย IRouteHandler has only one method that is GetHttpHandler().
Creating A Custom Route Handler
Create CustomeRouteHandler.cs file in Utility folder (It created for placing common functionality) and implement IRouteHandler interface as shown below.ย IRouteHandler interface contains GetHttpHandler method that returns IHttpHandler
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<span style="color: #000000;">using System.Web; using System.Web.Routing; namespace Dotnet_helpers.Utility { public class CustomRouteHandler : IRouteHandler { public IHttpHandler GetHttpHandler(RequestContext requestContext) { return new CustomHttpHandler(); } } public class CustomHttpHandler : IHttpHandler { public bool IsReusable { get { return false; } } public void ProcessRequest(HttpContext context) { context.Response.Redirect("https://www.dotnet-helpers.com", true); } } }</span> |
From the above code, we have returned CustomHttpHandler class. CustomHttpHandleris class which implements two methods IsReusable and ProcessRequest of IHttpHandler. ย IsReusable simply instruct the MVC framework whether this instance of IHttpHandler is reusable.ย The ProcessRequest method in this case simply redirects the user to https://www.dotnet-helpers.com website.ย
After defining the MyCustomRouteHandler, we need to add it to the RouteCollection under App_Start -> RouteConfig.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<span style="color: #000000;">public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.Add(new Route("NonAdminUser", new dotnet_helpers.Utility.CustomRouteHandler())); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } }</span> |
Run the application , when we hit the URL ย http://localhost:8080/NonAdminUser then it will redirect to theย https://www.dotnet-helpers.com.
Keep cool coding…
Very good blog post. I certainly love this website.
Thanks!
Thanks Nirman
Nice example with usability for extending IRouteHandler. Excellent!