ASP.NET 模块与处理程序

HttpHandler(处理程序) 和 HttpModule(托管模块)

ASP.NET其请求处理过程是基于管道模型的,这个管道模型由多个HttpModule和HttpHandler组成。

在整个生命周期中,它们大致的执行过程是这样的:client端发送页面请求,被IIS的某个进程截获,它根据申请的页面后缀(.aspx)不同,调用不同的页面处理程序(.asp->asp.dll;.aspx->ISAPI.dll),而页面处理程序在处理过程中,则要经历 HttpModule,HttpHandler的处理,前者HttpModule用于页面处理前和处理后的一些事件的处理,后者HttpHandler进行真正的页面处理。

     针对水印的全局实现

        Default.aspx页面代码:

[csharp]  view plain  copy
  1. <body>    
  2.     <form id="form1" runat="server">    
  3.     <div>    
  4.         <asp:Image ID="Image1" runat="server" ImageUrl="~/ProductImgs/1.jpg" />    
  5.         <asp:Image ID="Image2" runat="server" ImageUrl="~/ProductImgs/2.jpg" />    
  6.         <asp:Image ID="Image3" runat="server" ImageUrl="~/ProductImgs/3.jpg" />    
  7.         <asp:Image ID="Image4" runat="server" ImageUrl="~/ProductImgs/4.jpg" />    
  8.         <asp:Image ID="Image5" runat="server" ImageUrl="~/ProductImgs/default.jpg" />    
  9.     </div>    
  10.     </form>    
  11. </body>    

         Handler1.ashx页面:

        

[csharp]  view plain  copy
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Web;    
  5. using System.IO;    
  6. using System.Drawing;    
  7. using System.Drawing.Imaging;    
  8.     
  9. namespace WebApplication1    
  10. {    
  11.     /// <summary>    
  12.     /// Handler1 的摘要说明    
  13.     /// </summary>    
  14.     public class Handler1 : IHttpHandler    
  15.     {    
  16.         private string IMGS = "/~ProductImgs/";    
  17.         public void ProcessRequest(HttpContext context)    
  18.         {    
  19.             Image img;    
  20.             string path = context.Request.PhysicalPath;     
  21.             if (File.Exists(path))    
  22.             {    
  23.                 img=Image.FromFile(path);    
  24.                 Graphics graphics = Graphics.FromImage(img);    
  25.                 graphics.DrawString("版权所有",new Font("宋体",20),Brushes.Red,img.Width-50,img.Height-20);    
  26.                 graphics.Dispose();    
  27.             }    
  28.             else    
  29.             {    
  30.                 img = null;    
  31.             }    
  32.             context.Request.ContentType = "image/jpeg";    
  33.             img.Save(context.Response.OutputStream, ImageFormat.Jpeg);    
  34.             img.Dispose();    
  35.             context.Response.End();    
  36.         }    
  37.     
  38.         public bool IsReusable    
  39.         {    
  40.             get    
  41.             {    
  42.                 return false;    
  43.             }    
  44.         }    
  45.     }    
  46. }    

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值