Question

I have a generic handler (.ashx) in asp.net mvc3 web application. I use it to resize and cache images. but my Url is not clean (http://www.example.com/Thumb.ashx?img=someimage.jpg) I want to make it clean like http://www.example.com/Thumb/someimage.jpg how can I do it?

Can I maproute in global.asax, it yes then how? or should I use IIS 7 URL rewrite?

I appreciate any help, Thanks

Was it helpful?

Solution

After few hours of research I have done it using class (RouteHandler.cs) http handler but not with .ashx because .ashx can't be route using global.asax

public class RouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        HttpHanler httpHandler = new HttpHanler();
        return httpHandler;
    }
    public class HttpHanler : IHttpHandler
    {
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            var routeValues = context.Request.RequestContext.RouteData.Values;
            string file = context.Request.RequestContext.RouteData.Values["img"].ToString();

            // anything you can do here.

             context.Response.ContentType = "image/jpeg";
             context.Response.BinaryWrite("~/cat.jpg");
             context.Response.End();

        }
    }
}

then in global.asax just register a route

 routes.Add(new Route("Thumb/{img}", new RouteHandler()));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top