asp.net-mvc-3 - 用于调整图像大小的 MVC3 通用处理程序 (.ashx)(需要干净的 URL)

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

我在 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 重写?

非常感谢您的帮助,谢谢

最佳答案

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

关于asp.net-mvc-3 - 用于调整图像大小的 MVC3 通用处理程序 (.ashx)(需要干净的 URL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17811575/

相关文章:

asp.net-mvc-3 - FluentValidation-跨多个属性进行验证

c# - 为什么 ajax 在必要时返回值?

asp.net - HttpContext.Current.Response 和 Page.Response 有什么区别?

c# - 在 C# MVC 项目中将 session 添加到伪造的 httpContext

asp.net-mvc-3 - 隔离存储上的初始化失败

c# - 是否可以在 MVC3 (C#) 中为 `[Display(Name="Something")]` 数据注释使用变量

.htaccess - htaccess 重写破坏了相对路径

php - Codeigniter URL 重写

node.js - .htaccess 路由到 Node App

ASP.NET MVC - 单元测试,模拟 HttpContext 而不使用任何模拟框架