MVC-自訂HttpModule處理

來源:互聯網
上載者:User

標籤:

HttpModule是向實作類別提供模組初始化和處置事件。

當一個HTTP請求到達HttpModule時,整個ASP.NET Framework系統還並沒有對這個HTTP請求做任何處理,也就是說此時對於HTTP請求來講,HttpModule是一個HTTP請求的“必經之 路”,所以可以在這個HTTP請求傳遞到真正的請求處理中心(HttpHandler)之前附加一些需要的資訊在這個HTTP請求資訊之上,或者針對截獲 的這個HTTP請求資訊作一些額外的工作,或者在某些情況下乾脆終止滿足一些條件的HTTP請求,從而可以起到一個Filter過濾器的作用。

下面我們使用自訂的HttpModule模組在使用者作出請求進行截獲的這個HTTP請求資訊作一些額外的工作。

一.建立一個類庫項目,裡面添加我們的實體類,HttpModule繼承IHttpModule介面,實現裡面的Dispose方法,和Init事件註冊的方法。

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Web;namespace MyHttpModule{    public class HttpModule : IHttpModule    {        /// <summary>        /// 處置由實現 System.Web.IHttpModule 的模組使用的資源(記憶體除外)        /// </summary>        public void Dispose() { }        /// <summary>        /// 初始化模組,並使其為處理請求做好準備。        /// </summary>        /// <param name="context"></param>        public void Init(HttpApplication context)        {            context.BeginRequest += context_BeginRequest;//在 ASP.NET 響應請求時作為 HTTP 執行管線鏈中的第一個事件發生。            context.EndRequest += context_EndRequest;    //在 ASP.NET 響應請求時作為 HTTP 執行管線鏈中的最後一個事件發生。        }        /// <summary>        /// 在 ASP.NET 響應請求時作為 HTTP 執行管線鏈中的最後一個事件發生。        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        void context_EndRequest(object sender, EventArgs e)        {            HttpApplication application = sender as HttpApplication;            HttpContext context = application.Context;            HttpRequest request = application.Request;            HttpResponse response = application.Response;            response.Write("context_EndRequest >> 在 ASP.NET 響應請求時作為 HTTP 執行管線鏈中的最後一個事件發生");        }        /// <summary>        /// 在 ASP.NET 響應請求時作為 HTTP 執行管線鏈中的第一個事件發生。        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        void context_BeginRequest(object sender, EventArgs e)        {            HttpApplication application = sender as HttpApplication;            HttpContext context = application.Context;            HttpRequest request = application.Request;            HttpResponse response = application.Response;            response.Write("context_BeginRequest >> 在 ASP.NET 響應請求時作為 HTTP 執行管線鏈中的第一個事件發生");        }    }}
View Code

 

二.添加一個MVC的項目,裡面新增兩個控制器和視圖頁面

 

    public class HomeController : Controller    {        //        // GET: /Home/        public ActionResult Index()        {            return View();        }    }
View Code
    public class SonController : Controller    {        //        // GET: /Son/        public ActionResult Index()        {            return View();        }    }
View Code
@{    Layout = null;}<!DOCTYPE html><html><head>    <meta name="viewport" content="width=device-width" />    <title>Index</title></head><body>    <div>        進入Index頁面    </div></body></html>
View Code
@{    Layout = null;}<!DOCTYPE html><html><head>    <meta name="viewport" content="width=device-width" />    <title>Index</title></head><body>    <div>        進入Son頁面    </div></body></html>
View Code

IIS上發布成功,在Web.config中的<system.web>下添加配置項,將MyHttpModule.dll庫檔案放入bin目錄:

 

    <httpModules>      <!--<add name="類名" type="命名空間.類名" />      <add name="HttpModule" type="MyHttpModule.HttpModule"/>    </httpModules>

 

請求發布的地址,頁面效果如下:

 

MVC-自訂HttpModule處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.