自訂處理頁面請求

來源:互聯網
上載者:User

主要是繼承IHttpModule和IHttpHandler來重寫其 中的方法,IhttpModule 中重寫其Init方法及各種請示過程事件和Dispose方法

using System.Web;
using System;

namespace CustomerHttpModules
{
    /**//// <summary>
    /// Class1 的摘要說明。
    /// </summary>
    public class MyHttpModules:IHttpModule
    {
        public MyHttpModules()
        {
             
        }
        IHttpModule 成員#region IHttpModule 成員

        void System.Web.IHttpModule.Init(HttpApplication context)
        {
            context.BeginRequest+=new EventHandler(context_BeginRequest);
            context.EndRequest += new EventHandler(this.context_EndRequest);
        }
        public void context_BeginRequest(object obj,System.EventArgs e)
        {
            HttpApplication app = (HttpApplication)obj;
            app.Response.Write("Hi,testing customer http module request");
        }
        public void context_EndRequest(object obj,System.EventArgs e)
        {
            HttpApplication app=(HttpApplication)obj;
            app.Response.Write("Hi,end request here");
        }
        public void Dispose()
        {
            // TODO:  添加 MyHttpModules.Dispose 實現
        }

        #endregion
    }
}

 

然後產生dll,建立個asp.net應用,引用此dll,在web.config中加入

<httpModules>
  <add name="test" type="CustomerHttpModules.MyHttpModules,CustomerHttpModules"/>
 </httpModules>

則當有頁面請求時會尋找到此dll,然後執行其中過程。

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

以下為重寫IHttpHandler中方法的代碼:

同樣地建個類庫

using System;
using System.Web;

namespace CustomerHttpHandler
{
    /**//// <summary>
    /// Class1 的摘要說明。
    /// </summary>
    public class TestCustomerHttpHandler:IHttpHandler
    {
        public TestCustomerHttpHandler()
        {
            //
            // TODO: 在此處添加建構函式邏輯
            //
        }
        IHttpHandler 成員#region IHttpHandler 成員

        public void ProcessRequest(HttpContext context)
        {
             context.Response.Write("hi,I write something instead of what you input");
        }

        public bool IsReusable
        {
            get
            {
                // TODO:  添加 TestCustomerHttpHandler.IsReusable getter 實現
                return true;
            }
        }

        #endregion
    }
}

所有頁面請示的核心處理都是通過IHttpHandler的ProcessRequest方法來完成,因此只要我們重寫此方法,則無論頁面有任何請求,都會被我們所重寫的內容代替掉。嘿嘿

產生dll後再在asp.net應用中的 web.config 中加入

<httpHandlers>
  <add verb="*" path="*" type="CustomerHttpModules.MyHttpModules,CustomerHttpModules"/>
 </httpHandlers>

看看效果吧

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.