HttpModule與HttpHandler使用


HttpModule 使用


ASP.NET運行時在創建HttpApplication后,HttpApplication會根據它的Web.Config創建HttpModule,在創建HttpModule時,HttpApplication將調用HttpModule的Init方法。在Init方法中,可以訂閱多種HttpApplication事件,最常見是BeginRequest和EndRequest事件,它們是Http執行管線中的第一個和最后一個事件。


二級域名Cookie處理(所有以.cnblog.cn結尾的,共享Cookie資源)


先建一個類繼承IHttpModule接口:


public class CurrentCookieDomainModule : IHttpModule


然后在他的Init方法中訂閱EndRequest事件,以便在Http請求的最末端加入控制:


public void Init(HttpApplication context)
{
    context.EndRequest += new EventHandler(context_EndRequest);
}
void context_EndRequest(object sender ,EventArgs e)
{
    HttpContext context = ((HttpApplication)sender).Context;
    string domain = ".cnblog.cn";
    string cookieName = FormsAuthentication.FormsCookieName;
    HttpCookie cookie = context.Response.Cookie[cookieName];
    if(cookie != null)
    {
        cookie.Domain = domain;
    }
}


上面的代碼中我們FormAuthentication的Cookie的域改成.cnblog.cn,而不管它在什么二級域名中創建,要想代碼生效還要修改配置文件Web.Config:


    <system.web>
      <httpModules>
        <add name="CurrentCookieDomainModule " type="namespace.CurrentCookieDomainModule"/>
      </httpModules>
    </system.web>


HttpHandler使用


HttpHandler就是最終響應Http請求,生成Http響應的處理器,它們實例由ASP.NET運行時創建,並生存在運行時環境中。ASP.NET處理一次Http請求,可能會有多個HttpModule來參與,但是一般只有一個HttpHandler來參與處理請求的工作。什么時候需要定義HttpHandler呢,一般我們響應給客戶的是一個HTML頁面,這種情況System.Web.UI.Page類中的默認的HttpHandler完全可以勝任,但是有時候我們響應用戶德不是一個HTML,而是XML或者是圖片的時候,自定義的Httphandler也是不錯的選擇。


使用場景:給圖片的左下角加上文字


使用HttpHandler可以很容易實現這個功能。
我們先定義一個實現IHttpHandler接口的ImageHandler來處理生成圖片的請求:
public class ImageHandler : IHttpHandler
這個Handler處理是可以重用多次請求處理的,所以IsReusable返回true:


        public bool IsReusable
        {
            get { return true; }
        }


我們實現一個獲取背景圖片的靜態方法:


        private static Image _originalImage = null;
        private static Image GetOriginalImage(HttpContext context)
        {
            if (_originalImage == null)
            {
                _originalImage = Image.FromFile(context.Server.MapPath("~/Image/2012-2-7 14-00-50.jpg"));
            }
            return _originalImage.Clone() as Image;
        }

最后我們實現ProcessRequest(HttpContext context)方法,在背景圖片上加上文字,並輸出:


        public void ProcessRequest(HttpContext context)
        {
            Image originalImage = GetOriginalImage(context);
            Graphics graphic = Graphics.FromImage(originalImage);
            Font font = new Font("幼圓",24.0f,FontStyle.Regular);
            string query = HttpUtility.UrlDecode(context.Request.QueryString["query"]);
            graphic.DrawString(query, font, new SolidBrush(Color.MediumSeaGreen), 20.0f, originalImage.Height - font.Height - 10);
            originalImage.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
            graphic.Dispose();
            originalImage.Dispose();
        }

另外我們還要配置Web.Config文件:


      <httpHandlers>
          <add verb="*" path="*.jpg" type="ImageHandler.ImageHandler"/>
      </httpHandlers>

我們在瀏覽器中訪問:http://localhost:25868/2012-2-7%2014-00-50.jpg?query=hello

可以看到返回的圖片:




我們可以看到hello字樣已經成功輸出。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM