Use URL Routing with custom handlers

Description and samples
Table of Contents
Description

Instead of an  ASP.NET page well-located on the Web application, a custom HTTP Handler class can be build from scratch. This handler will execute custom actions as needed and at the end generate a response for the caller.

For more information on HTTP Handlers, see HTTP Handler description article.

Operations
Implementing URL Routing with a custom handler

In the code presented next, the globalization file “global.asax” register routes, where all the virtual URLs are mapped to one or more custom handler.

How to do it
void Application_Start(object sender, EventArgs e)
{
   // Register routes...
   Route echoRoute = new Route("echo/{message}", new EchoRouteHandler());
   RouteTable.Routes.Add(echoRoute);
}
public class EchoRouteHandler : IRouteHandler
{
   public IHttpHandler GetHttpHandler(RequestContext requestContext)
   {
      return new EchoHandler();
   }
}
public class EchoHandler : IHttpHandler
{
   public bool IsReusable
   {
      get { return false; }
   }

   public void ProcessRequest(HttpContext context)
   {
      string messageToEcho = context.Request.RequestContext.RouteData.Values["message"] as string;

      context.Response.Write(messageToEcho);
   }
}
References

[1] Refer to this article for more complete samples