欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP.NET處理HTTP請求的流程:IHttpModule、IHttpHandler與管道事件

 更新時間:2022年05月12日 16:42:18   作者:springsnow  
這篇文章介紹了ASP.NET處理HTTP請求的流程:IHttpModule、IHttpHandler與管道事件,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、ASP.NET處理管道

  • Asp.net處理管道的第一步是創(chuàng)建HttpWorkerRequest對象,它包含于當(dāng)前請求有關(guān)的所有信息。
  • HttpWorkerRequest把請求傳遞給HttpRuntime類的靜態(tài)ProcessRequest方法。HttpRuntime首先要做的事是創(chuàng)建HttpContext對象,并用HttpWorkerRequest進(jìn)行初始化。
  • 創(chuàng)建了HttpContext實(shí)例之后,HttpRuntime類就通過調(diào)用HttpApplicationFactory的靜態(tài)GetApplicationInstance()方法,為該應(yīng)用程序請求HttpApplication派生類的一個示例。GetApplicationInstance()方法要么創(chuàng)建一個HttpApplication類的一個新實(shí)例,要么從應(yīng)用程序?qū)ο蟪刂腥〕鲆粋€實(shí)例。
  • 在創(chuàng)建完成HttpApplication實(shí)例之后,就對它進(jìn)行初始化,并在初始化期間分配應(yīng)用程序定義的所
  • 有模塊。模塊式實(shí)現(xiàn)IHttpModule接口的類,作用就是為了實(shí)現(xiàn)那經(jīng)典的19個標(biāo)準(zhǔn)處理事件。
  • 在創(chuàng)建了模塊之后,HttpRuntime類通過調(diào)用它的BeginProcessRequest方法,要求最新檢索到的HttpApplication類對當(dāng)前請求提供服務(wù)。然后,為當(dāng)前請求找到合適的處理程序工廠。
  • 創(chuàng)建處理程序,傳遞當(dāng)前HttpContext,一旦ProcessRequest方法返回,請求完成。

二、IHttpHandler

HttpHandler是asp.net真正處理Http請求的地方。在這個HttpHandler容器中,ASP.NET Framework才真正地對客戶端請求的服務(wù)器頁面做出編譯和執(zhí)行,并將處理過后的信息附加在HTTP請求信息流中再次返回到HttpModule中。

當(dāng)一個HTTP請求經(jīng)過HttpModule容器傳遞到HttpHandler容器中時,ASP.NET Framework會調(diào)用HttpHandler的ProcessRequest成員方法來對這個HTTP請求進(jìn)行真正的處理。并將處理完成的結(jié)果繼續(xù)經(jīng)由HttpModule傳遞下去,直至到達(dá)客戶端。

HttpHandler與HttpModule不同,一旦定義了自己的HttpHandler類,那么它對系統(tǒng)的HttpHandler的關(guān)系將是“覆蓋”關(guān)系。

應(yīng)用1:圖片防盜鏈(實(shí)現(xiàn)一個自定義的IHttpHandler)

第一:定義一個實(shí)現(xiàn)了IHttpHandler的類,并且實(shí)現(xiàn)其ProcessRequest方法。在一個HttpHandler容器中如果需要訪問Session,必須實(shí)現(xiàn)IRequiresSessionState接口,這只是一個標(biāo)記接口,沒有任何方法。

public class PictureHttpHandler : IHttpHandler
{
    public bool IsReusable
    {
        get { return true; }
    }

    public void ProcessRequest(HttpContext context)
    {
        //站點(diǎn)的域名
        string myDomain = "localhost";

        if (context.Request.UrlReferrer == null ||
            context.Request.UrlReferrer.Host.ToLower().IndexOf(myDomain) < 0)
        {
            //如果是通過瀏覽器直接訪問或者是通過其他站點(diǎn)訪問過來的,則顯示“資源不存在”圖片
            context.Response.ContentType = "image/JPEG";
            context.Response.WriteFile(context.Request.PhysicalApplicationPath + "/images/noimg.jpg");
        }
        else
        {
            //如果是通過站內(nèi)訪問的,這正常顯示圖片
            context.Response.ContentType = "image/JPEG";
            context.Response.WriteFile(context.Request.PhysicalPath);
        }

    }
}

第二:在web.config中注冊這個類,并且指定Handler處理的請求類型,把此節(jié)點(diǎn)插入system.web節(jié)點(diǎn)中

<httpHandlers>
      <!--path中指定的是執(zhí)行type中HttpHandler的訪問路徑。此路徑可以帶后綴也可以不帶后綴。如果path配置為*,則會對所有的請求執(zhí)行此HttpHandler-->
     <add  verb="*" path="*.jpg" type="MyHttpHandler.PictureHttpHandler,MyHttpHandler"/>
</httpHandlers>

正常訪問default頁面時:

通過圖片地址直接訪問時:

應(yīng)用2、生成驗(yàn)證碼

public class ValidateCodeHttpHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "image/gif";
        //建立Bitmap對象,繪圖        
        Bitmap basemap = new Bitmap(200, 60);
        Graphics graph = Graphics.FromImage(basemap);
        graph.FillRectangle(new SolidBrush(Color.White), 0, 0, 200, 60);
        Font font = new Font(FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel);
        Random r = new Random();
        string letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ";
        string letter;
        StringBuilder s = new StringBuilder();
        //添加隨機(jī)的五個字母        
        for (int x = 0; x < 5; x++)
        {
            letter = letters.Substring(r.Next(0, letters.Length - 1), 1);
            s.Append(letter);
            graph.DrawString(letter, font, new SolidBrush(Color.Black), x * 38, r.Next(0, 15));
        }
        //混淆背景        
        Pen linePen = new Pen(new SolidBrush(Color.Black), 2);
        for (int x = 0; x < 6; x++)
            graph.DrawLine(linePen, new Point(r.Next(0, 199), r.Next(0, 59)),
                new Point(r.Next(0, 199), r.Next(0, 59)));
        //將圖片保存到輸出流中              
        basemap.Save(context.Response.OutputStream, ImageFormat.Gif);
        //context.Session["CheckCode"] = s.ToString();   
        //如果沒有實(shí)現(xiàn)IRequiresSessionState,則這里會出錯,也無法生成圖片         
        context.Response.End();
    }
    public bool IsReusable
    {
        get { return true; }
    }
}

把下面的項(xiàng)加到web.config中的httphandler節(jié)點(diǎn)中:

<add  verb="*" path="validatevode" type="MyHttpHandler.ValidateCodeHttpHandler,MyHttpHandler"/>

訪問validatevode時:

三、自定義HttpModule:

每次請求的開始和結(jié)束定義的HttpModule。

在Asp.net中,創(chuàng)建在System.Web命名空間下的IHttpModule接口專門用來定義HttpApplication對象的事件處理。實(shí)現(xiàn)IHttpModule接口的類稱為HttpModule(Http模塊)。

1、通過IHttpModule創(chuàng)建HttpApplication的事件處理程序

public class ModuleExample : IHttpModule
{
    public void Init(System.Web.HttpApplication application)
    {
        application.PostAuthenticateRequest += (sender, args) =>
         {
             HttpContext context = ((HttpApplication)sender).Context;
             context.Response.Write("請求PostAuthenticate");
         };

        application.BeginRequest += (sender, args) =>
        {
            HttpContext context = ((HttpApplication)sender).Context;
            context.Response.Write("請求到達(dá)");
        };

        application.EndRequest += (sender, args) =>
        {
            HttpContext context = ((HttpApplication)sender).Context;
            context.Response.Write("請求結(jié)束");
        };
    }
    public void Dispose()
    {
        throw new NotImplementedException();
    }
}

2、注冊HttpModule

在Asp.net中,實(shí)現(xiàn)IHttpModule接口只是實(shí)現(xiàn)HttpModule的第一步,在Asp.net中所使用的HttpModule還必須在網(wǎng)站配置文件中進(jìn)行注冊才能真正生效,并在Asp.net中使用。

<system.webServer>
    <modules>
      <add name="ModuleExample" type="Samples.ModeleExample">
    </modules>
</system.webServer>

3、常見的HttpModule

在Asp.net中,已經(jīng)預(yù)定義了許多HttpModule,甚至已經(jīng)在服務(wù)器的網(wǎng)站配置文件中進(jìn)行了注冊,在系統(tǒng)文件夾C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config中看到已經(jīng)注冊的HttpModule如下:

<httpModules>
    <add name="OutputCache" type="System.Web.Caching.OutputCacheModule" />
    <add name="Session" type="System.Web.SessionState.SessionStateModule" />
    <add name="WindowsAuthentication" type="System.Web.Security.WindowsAuthenticationModule" />
    <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />
    <add name="PassportAuthentication" type="System.Web.Security.PassportAuthenticationModule" />
    <add name="RoleManager" type="System.Web.Security.RoleManagerModule" />
     <add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />
     <add name="FileAuthorization" type="System.Web.Security.FileAuthorizationModule" />
     <add name="AnonymousIdentification" type="System.Web.Security.AnonymousIdentificationModule" />
     <add name="Profile" type="System.Web.Profile.ProfileModule" />
     <add name="ErrorHandlerModule" type="System.Web.Mobile.ErrorHandlerModule, System.Web.Mobile, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
     <add name="ServiceModel" type="System.ServiceModel.Activation.HttpModule, System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
     <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" />
     <add name="ScriptModule-4.0" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
 </httpModules>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • ASP.NET Core基礎(chǔ)之啟動設(shè)置

    ASP.NET Core基礎(chǔ)之啟動設(shè)置

    這篇文章介紹了ASP.NET Core基礎(chǔ)之啟動設(shè)置,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-02-02
  • .NET中開源文檔操作組件DocX的介紹與使用

    .NET中開源文檔操作組件DocX的介紹與使用

    在大家日常開發(fā)中讀寫Offic格式的文檔,大家多少都有用到,可能方法也很多,組件有很多。這里不去討論其他方法的優(yōu)劣,只是向大家介紹一款開源的讀寫word文檔的組件。讀寫Excel有NPOI,讀寫Word,那看看DocX吧。下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。
    2016-12-12
  • asp.net Webconfig中的一些配置

    asp.net Webconfig中的一些配置

    除了手動編輯此文件以外,您還可以使用Web 管理工具來配置應(yīng)用程序的設(shè)置。可以使用 Visual Studio 中的“網(wǎng)站”->“Asp.Net 配置”選項(xiàng)。
    2010-07-07
  • ASP.NET?Core?Razor頁面用法介紹

    ASP.NET?Core?Razor頁面用法介紹

    這篇文章介紹了ASP.NET?Core?Razor頁面的用法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-02-02
  • ADO.NET 連接數(shù)據(jù)庫字符串小結(jié)(Oracle、SqlServer、Access、ODBC)

    ADO.NET 連接數(shù)據(jù)庫字符串小結(jié)(Oracle、SqlServer、Access、ODBC)

    ADO.NET 連接數(shù)據(jù)庫字符串小結(jié),包括Oracle、SqlServer、Access、ODBC,需要的朋友可以收藏下
    2012-04-04
  • C#枚舉的高級應(yīng)用

    C#枚舉的高級應(yīng)用

    這篇文章介紹了C#枚舉的高級應(yīng)用,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • ASP.NET中Label控件用法詳解

    ASP.NET中Label控件用法詳解

    本文主要介紹Label控件的詳細(xì)用法,雖然很基礎(chǔ),但是我感覺很有必要,希望對大家有所幫助。
    2016-04-04
  • ASP.NET Core MVC中的控制器(Controller)介紹

    ASP.NET Core MVC中的控制器(Controller)介紹

    這篇文章介紹了ASP.NET Core MVC中的控制器(Controller),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • c#對xml的簡單操作

    c#對xml的簡單操作

    c#對xml的簡單操作...
    2006-08-08
  • SQL Server 2005 RTM 安裝錯誤 :The SQL Server System Configuration Checker cannot be executed due to

    SQL Server 2005 RTM 安裝錯誤 :The SQL Server System Configuratio

    SQL Server 2005 RTM 安裝錯誤 :The SQL Server System Configuration Checker cannot be executed due to...
    2007-02-02

最新評論