Fork me on GitHub
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字样已经成功输出。

posted on 2012-07-11 17:45  HackerVirus  阅读(88)  评论(0编辑  收藏  举报