• Handler


    httphandler就是来处理客户端对服务器端请求的中转站 后缀名是ashx


    案例

    namespace BookShop.Handler
    {
    /// <summary>
    /// BookHandler 的摘要说明
    /// </summary>
    public class BookHandler : IHttpHandler
    {
    public void ProcessRequest(HttpContext context)
    {
    string fileName = context.Request.Url.ToString(); //获取请求的地址
    fileName = fileName.Substring(fileName.LastIndexOf('/') + 1);
    string bookPic = context.Server.MapPath("/images/BookCovers/" + fileName);
    Image book;
    if (File.Exists(bookPic))
    {
    //读取对应的图片
    book = Image.FromFile(bookPic);
    //读取水印图片
    Image water = Image.FromFile(context.Server.MapPath("/images/WaterMark.jpg"));
    //画布
    Graphics g = Graphics.FromImage(book);
    //在画布上❀水印
    g.DrawImage(water, book.Width - water.Width, book.Height - water.Height);
    g.Dispose(); //释放资源
    water.Dispose(); //释放
    
    }
    else
    {
    //如果请求图书不存在,返回默认图片
    book = Image.FromFile(context.Server.MapPath("/Images/default.jpg"));
    }
    context.Response.ContentType = "image/jpeg"; //设置输出格式
    //向客户端输出图片流
    book.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    book.Dispose(); //释放
    }
    
    public bool IsReusable
    {
    get
    {
    return false;
    }
    }
    }
    }

    handler配置 system.webServer

    <system.webServer>
    <handlers><!--配置handler-->
    <!--type属性需要明确命名空间-->
    <add name="bookWater" verb="*" path="/images/BookCovers/*.jpg" type="BookShop.Handler.BookHandler"/>
    </handlers>
    </system.webServer>

    客户端通过浏览器向服务器发送请求中间由aspnet_isapi.dll 再有Application 再由WebModule 再由WebHandler给向客户端

    配置项中 谁访问了Path中 那么就要去WebModule 中进行处理

  • 相关阅读:
    关于软件工程的疑问
    thinkphp基础入门(2)
    JS What does `void 0` mean?
    第2章 TCPIP的工作方式
    JavaScript中var a=b=c=d的发现
    如何让 Git 忽略掉文件中的特定行内容?
    thinkphp基础入门(1)
    IIS网站服务器性能优化指南(转载)
    240个jquery插件
    解决_动态加载JS文件_调用时出错
  • 原文地址:https://www.cnblogs.com/ZaraNet/p/9433529.html
Copyright © 2020-2023  润新知