MVC3 generic handler (.ashx) for Images resizing (Need clean URL)

邮差的信 提交于 2019-12-12 17:29:57

问题


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


回答1:


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()));


来源:https://stackoverflow.com/questions/17811575/mvc3-generic-handler-ashx-for-images-resizing-need-clean-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!