In the last two articles, we learnt different types of convention based routing and attribute routing. In this article, we shall learn how to create a custom route handler in ASP.NET MVC that helps us to redirect user to a particular location based on a specific url typed.
Introduction
We have already seen the strength of ASP.NET MVC framework in previous two articles.
ASP.NET MVC framework also let us define our own route handler by implementing
IRouteHandler
interface that exists in
System.Web.Routing namespace.
Creating custom route handler in ASP.NET MVC
To create a custom route handler, we will create a new class file (in this case we have created this class file in
Utility folder of our project).
using System.Web.Routing;
namespace MVCTraining5.Utility
{
public class MyCustomRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return new MyClassHttpHandler();
}
}
// write code for MyClassHttpHandler class here
}
Notice that we have inherited the class with IRouteHandler
interface and implementing its GetHttpHandler
method that is responsible to get the Http handler when this route handler is called.
Now in the same .cs file, we will create another class called
MyClassHttpHandler
class and its code looks like below
public class MyClassHttpHandler : IHttpHandler
{
public bool IsReusable
{
get
{
return false;
}
}
public void ProcessRequest(HttpContext context)
{
context.Response.Redirect("http://www.itfunda.com", true);
}
}
Notice that GetHttpHanlder
method returns the instance of this class and this class also implements IHttpHanlder
interface that forces us to implement ProcessRequest
method. In this method, we are simply redirecting the user to http://www.itfunda.com. The other IsReusable method simply returns false that indicates that another request can not use this instance of IHttpHandler.
In real time application, we can call action methods of controllers and return proper response from ProcessRequest
method, however here for simplicity we have just redirected to itfunda.com website.
How to register custom route handler class?
To register custom route handler, we can write below code at the starting of the RegisterRoutes
method in App_Start/RouteConfig.cs file.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new Route("itfunda", new MVCTraining5.Utility.MyCustomRouteHandler()));
Here, the first parameter is the value of url we want to handle and second parameter is the route handler to be called when this pattern of url is found.
Testing custom route handler
To test the custom route handler; we need to type http://localhost:63087/itfunda in the browser address bar and the browser will get redirected to http://www.itfunda.com !
Notice the first parameter of the new Route(), it is itfunda and the same has to be mentioned in the url so that this request is handled through MyCustomRouteHandler class.
Conclusion
Custom route handler gives us immense flexibility to in routing urls in ASP.NET MVC. It can be used to support old urls, redirect user to a particular website of the company, giving a short url as replacement of a bigger url and in many more ways.
Hope this article was useful, do write your feedback or comment. Thanks for reading.