2

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

4

1 回答 1

3

经过几个小时的研究,我使用类(RouteHandler.cs)http处理程序完成了它,但没有使用.ashx,因为.ashx不能使用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();

        }
    }
}

然后在 global.asax 中注册一条路线

 routes.Add(new Route("Thumb/{img}", new RouteHandler()));
于 2013-07-27T01:32:26.560 回答