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