zoukankan      html  css  js  c++  java
  • httphandlers 与 httpmodules

    httphandlers 与 httpmodules 

    最近在asp.net下用httphandles实现urlrewrite,搜了一些资料,以备以后查看:

    HttpHandlers 和 HttpModules 通过扩展原始的页面框架,提供了用于处理请求和响应的两种途径。HttpHanders的主要目的是处理对某种特定文件或者在URL中对某个文件路径的请求,而HttpModule则主要被用于在最开始的阶段处理一个请求以及在最后阶段处理一个响应。

        HttpHandlers是指实现了IHttpHandler接口的类。它们在ProcessRequest()方法中获得对当前HttpContext 对象的引用,并能依靠HttpContext对象的属性来执行代码。典型的例子是,HttpHandler分析来自Request属性(对象)的数据并通过Response属性(对象)发送回某些东西。HttpHandler同时实现了一个IsReusable属性,它告诉Asp.net是否类的同一个实例能够用于处理并发(subsequent)的请求

    第一步:首先新建一个类库项目:

    using System;
    using System.Web;

    public class Handler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.Write("aaa");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }

    第二步:在web项目中引用此dll

    第三步:在Web.Config中进行配置,type参数为:命名空间名.类名,程序集名

    <httpHandlers>
            <add verb="*" path="*.aspx" type="Handler,ClassLibrary1" />
    </httpHandlers>

    好了,现在你不论访问哪一个aspx文件,返回的都是aaa了!

        HttpModules通过添加事件处理器到应用程序的事件,从而代码可以同应用程序进行交互。这些类都必须实现IHttpModule接口。 Init()方法为我们提供了对一个应用程序的引用,在那个方法中我们可以绑定事件处理器到应用程序的事件上。事件处理器通过实现基本的事件处理器标记式来同应用程序进行交互。HttpModule也实现了一个Dispose()方法,它被用于在应用程序结束时执行必需的清理工作。

    第一步:首先新建一个类库项目:

    namespace ClassLibrary1
    {
        class Class2:IHttpModule
        {
            public void Dispose()
            {
                //throw new Exception("The method or operation is not implemented.");
            }

            public void Init(HttpApplication context)
            {
                context.Context.Cache["a"] = "a";
            }

        }
    }

    第二步:在Web应用程序中添加此引用

    第三步:在Web.Config中进行配置,name随便起,type格式为:命名空间名.类名

    <httpModules>
        <add name="a" type="ClassLibrary1.Class2"/>
       </httpModules>

    第四步:在web应用程序中验证
    if (Cache["a"] != null)
    {
        Response.Write(Cache["a"].ToString());
    }
    else
    {
        Response.Write("no");
    }

    可以看到,打印的是a

    其实,HttpModule是在HttpHandler之前就响应的,所以可以影响后者。

    一个httpmodles例子

    using System;
    using System.Web;
    public class HelloWorldModule : IHttpModule
    {
        public HelloWorldModule()
        {
        }

        public String ModuleName
        {
            get { return "HelloWorldModule"; }
        }

        // In the Init function, register for HttpApplication
        // events by adding your handlers.
        public void Init(HttpApplication application)
        {
            application.BeginRequest +=
                (new EventHandler(this.Application_BeginRequest));
            application.EndRequest +=
                (new EventHandler(this.Application_EndRequest));
        }

        private void Application_BeginRequest(Object source,
             EventArgs e)
        {
            // Create HttpApplication and HttpContext objects to access
            // request and response properties.
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;
            context.Response.Write("<h1><font color=red> HelloWorldModule: Beginning of Request</font></h1><hr>");
        }

        private void Application_EndRequest(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;
            context.Response.Write("<hr><h1><font color=red>HelloWorldModule: End of Request</font></h1>");
        }

        public void Dispose()
        {
        }
    }

  • 相关阅读:
    PLSQL Developer使用技巧整理
    PLSQL DEVELOPER 使用的一些技巧【转】 .
    MYEclipse Available Memory is low 警告 解决方法
    myeclipse安装svn插件的多种方式
    MySql的存储过程和触发器
    springmvc学习及源码地址
    spring源码下载链接
    struts2源码下载链接
    个人总结的常用java,anroid网站
    Java生成扫描可以生成手机号名片的二维码
  • 原文地址:https://www.cnblogs.com/armyfai/p/2758496.html
Copyright © 2011-2022 走看看