【问题标题】:MVC3 generic handler (.ashx) for Images resizing (Need clean URL)用于调整图像大小的 MVC3 通用处理程序 (.ashx)(需要干净的 URL)
【发布时间】:2013-07-23 13:28:01
【问题描述】:

我在 asp.net mvc3 Web 应用程序中有一个通用处理程序 (.ashx)。我用它来调整大小和缓存图像。但我的网址不干净 (http://www.example.com/Thumb.ashx?img=someimage.jpg) 我想让它像 http://www.example.com/Thumb/someimage.jpg 一样干净 我该怎么做?

我可以在 global.asax 中映射路由吗?还是应该使用 IIS 7 URL 重写?

感谢您的帮助,谢谢

【问题讨论】:

  • 伙计们,请给我一些建议..

标签: asp.net-mvc-3 url-rewriting httpcontext generic-handler


【解决方案1】:

经过几个小时的研究,我使用类 (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()));

【讨论】:

    猜你喜欢
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 2012-02-02
    相关资源
    最近更新 更多