Sunday 26 June 2016

IHttpHandlers.ashx routing in [Solved]

I am using RegisterRoutes(RouteCollection routes)
I have 3 types of files

  1. .aspx
  2. .ashx 
  3. .svc
  4. .asmx

what is working for me are
For .aspx
routes.MapPageRoute("Static home", "", "~/pages/home/default.aspx");

For .svc
WebServiceHostFactory factory = new WebServiceHostFactory();
RouteTable.Routes.Add(new ServiceRoute("op/api/v1.0", factory,
typeof(common.Getway))); // for service contact that defined in .svc.cs file

For .ashx 
But i was stuck with .ashx file and what worked for me is

RouteTable.Routes.Add(new Route("op/playx", new HttpHandlerRoute("~/media/play.ashx")));

public class HttpHandlerRoute : IRouteHandler

        private String _VirtualPath = null;

        public HttpHandlerRoute(String virtualPath)
            _VirtualPath = virtualPath;

        public IHttpHandler GetHttpHandler(RequestContext requestContext)
            IHttpHandler httpHandler = (IHttpHandler)System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(_VirtualPath, typeof(IHttpHandler));
            return httpHandler;

For .asmx

 RouteTable.Routes.Add(new Route("op/contacts", new ServiceRouteHandler("~/webservices/contact.asmx")));

 public class ServiceRouteHandler : IRouteHandler
        private readonly string _virtualPath;
        private readonly System.Web.Services.Protocols.WebServiceHandlerFactory _handlerFactory = new System.Web.Services.Protocols.WebServiceHandlerFactory();

        public ServiceRouteHandler(string virtualPath)
            if (virtualPath == null)
                throw new ArgumentNullException("virtualPath");
            if (!virtualPath.StartsWith("~/"))
                throw new ArgumentException("Virtual path must start with ~/", "virtualPath");
            _virtualPath = virtualPath;

        public IHttpHandler GetHttpHandler(RequestContext requestContext)
            return _handlerFactory.GetHandler(HttpContext.Current, requestContext.HttpContext.Request.HttpMethod, _virtualPath, requestContext.HttpContext.Server.MapPath(_virtualPath));

Routing asmx, routing ashx, routing wcf, routing web form