HttpHandler

概述

HttpHandler是一個HTTP請求的真正處理中心,也正是在這個HttpHandler容器中,ASP.NET Framework才真正地對客戶端請求的服務器頁面做出編譯和執行,並將處理過後的信息附加在HTTP請求信息流中再次返回到HttpModule中。

 

IHttpHandler是什麼

IHttpHandler定義瞭如果要實現一個HTTP請求的處理所必需實現的一些系統約定。HttpHandlerHttpModule不同,一旦定義了自己的HttpHandler類,那麼它對系統的HttpHandler的關係將是“覆蓋”關係。

 

IHttpHandler如何處理HTTP請求

當一個HTTP請求經同HttpModule容器傳遞到HttpHandler容器中時,ASP.NET Framework會調用HttpHandlerProcessRequest成員方法來對這個HTTP請求進行真正的處理。以一個ASPX頁面爲例,正是在這裏一個ASPX頁面才被系統處理解析,並將處理完成的結果繼續經由HttpModule傳遞下去,直至到達客戶端。

對於ASPX頁面,ASP.NET Framework在默認情況下是交給System.Web.UI.PageHandlerFactory這個HttpHandlerFactory來處理的。所謂一個HttpHandlerFactory,所謂一個HttpHandlerFactory,是指當一個HTTP請求到達這個HttpHandler Factory時,HttpHandlerFactory會提供出一個HttpHandler容器,交由這個HttpHandler容器來處理這個HTTP請求。

一個HTTP請求都是最終交給一個HttpHandler容器中的ProcessRequest方法來處理的。


1ProcessRequest方法

 


一個簡單的
HttpHandler容器

通過實現IHttpHandler接口可以創建自定義HTTP處理程序,該接口只包含兩個方法。通過調用IsReusableIHttpHandlerFactory可以查詢處理程序以確定是否可以使用同一實例爲多個請求提供服務。ProcessRequest方法將HttpContext實例用作參數,這使它能夠訪問RequestResponse內部對象。在一個HttpHandler容器中如果需要訪問Session,必須實現IRequiresSessionState接口,這只是一個標記接口,沒有任何方法。


示例
1

using System;

using System.Collections.Generic;

using System.Text;

using System.Web;

using System.Web.SessionState;

 

namespace MyHandler

{

    /// <summary>

    /// 目的:實現一個簡單的自定義HttpHandler容器

    /// 作者:文野

    /// 聯繫:[email protected]

    /// </summary>

    public class MyFirstHandler : IHttpHandler,IRequiresSessionState

    {

        #region IHttpHandler 成員

 

        public bool IsReusable

        {

            get { return true; }

        }

 

        public void ProcessRequest(HttpContext context)

        {

            context.Response.Write("<h1><b>Hello HttpHandler</b></h1>");

            context.Session["Test"] = "測試HttpHandler容器中調用Session";

            context.Response.Write(context.Session["Test"]);

        }

 

        #endregion

    }

}

Web.config中加入如下配置:

<httpHandlers>

     <add verb="*" path="*" type="MyHandler.MyFirstHandler, MyHandler"/>

</httpHandlers>


 

IHttpHandler工廠

ASP.NET Framework實際不直接將相關的頁面資源HTTP請求定位到一個其內部默認的IHttpHandler容器之上,而定位到了其內部默認的IHttpHandler工廠上。IHttpHandler工廠的作用是對IHttpHandler容器進行調度和管理。

IHttpHandlerFactory接口包含兩個方法。GetHandler返回實現IHttpHandler接口的類的實例,ReleaseHandler使工廠可以重用現有的處理程序實例。

 

示例2

using System;

using System.Collections.Generic;

using System.Text;

using System.Web;

 

namespace MyHandler

{

    public class MyHandlerFactory : IHttpHandlerFactory

    {

        #region IHttpHandlerFactory 成員

 

        public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)

        {

            string fname = url.Substring(url.IndexOf('/') + 1);

            while (fname.IndexOf('/') != -1)

                fname = fname.Substring(fname.IndexOf('/') + 1);

            string cname = fname.Substring(0, fname.IndexOf('.'));

            string className = "MyHandler." + cname;

 

            object h = null;

 

            try

            {

                // 採用動態反射機制創建相應的IHttpHandler實現類。

                h = Activator.CreateInstance(Type.GetType(className));

            }

            catch (Exception e)

            {

                throw new HttpException("工廠不能爲類型"+cname+"創建實例。",e);

            }

 

            return (IHttpHandler)h;

        }

 

        public void ReleaseHandler(IHttpHandler handler)

        {

           

        }

 

        #endregion

    }

 

    public class Handler1 : IHttpHandler

    {

        #region IHttpHandler 成員

 

        public bool IsReusable

        {

            get { return true; }

        }

 

        public void ProcessRequest(HttpContext context)

        {

            context.Response.Write("<html><body><h1>來自Handler1的信息。</h1></body></html>");

        }

 

        #endregion

    }

 

    public class Handler2 : IHttpHandler

    {

        #region IHttpHandler 成員

 

        public bool IsReusable

        {

            get { return true; }

        }

 

        public void ProcessRequest(HttpContext context)

        {

            context.Response.Write("<html><body><h1>來自Handler2的信息。</h1></body></html>");

        }

 

        #endregion

    }

}


HttpHandler 可以通過以上配置的方式來使用, 也可以通過一個頁面中的某個方法WEB調用來使用:

例:

 AsyncNorthwindImageGrabber.ashx:

<%@ WebHandler Language="C#" Class="AsyncNorthwindImageGrabber" %>

using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Data;
using System.Data.SqlClient;
using System.Web.Caching;
using System.Configuration;
using System.Web.Configuration;
using System.IO;

//IHttpAsyncHandler: 定義HTTP異步處理程序對象必須實現的協議
//IAsyncResult: 異步處理的結果
//AsyncCallback: 異步的回調方法
public class AsyncNorthwindImageGrabber : IHttpAsyncHandler
{
    private SqlConnection _connection;
    private SqlCommand _command;
    private HttpContext _context;

    public void ProcessRequest (HttpContext context)
    {
        // Never called
    }
       
    public bool IsReusable
    {
        get { return true; }
    }

    public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object state)
    {
        // Get the employee ID from the query string
        string _id = context.Request["ID"];
        if (String.IsNullOrEmpty(_id))
            return null;

        // Make sure the employee ID is numeric as an anti-hack measure
        int id = -1;
        try
        {
            id = Convert.ToInt32(_id);
        }       
        catch (FormatException)
        {
            return null;
        }

        // Save a reference to the HttpContext
        _context = context;

        // Get the connection string
        string connect = context.Request["ConnectionString"];
        if (String.IsNullOrEmpty(connect))
            return null;

        string conn = WebConfigurationManager.ConnectionStrings[connect].ConnectionString;

        // Retrieve the image from the database
        _connection = new SqlConnection (conn);
        _connection.Open ();
        _command = new SqlCommand ("SELECT Photo FROM Employees WHERE EmployeeID=@id", _connection);
        _command.Parameters.Add("id", SqlDbType.Int).Value = id;
        return _command.BeginExecuteReader (cb, state);
    }

    public void EndProcessRequest(IAsyncResult ar)
    {
        try
        {
            SqlDataReader reader = _command.EndExecuteReader(ar);

            if (reader != null && reader.HasRows)
            {
                // Retrieve the image returned in the query
                reader.Read();
                byte[] image = (byte[])reader[0];

                // Return the image in the HTTP response
                _context.Response.ContentType = "image/jpeg";
                _context.Response.OutputStream.Write(image, 78, image.Length - 78);
            }
        }
        finally
        {
            // Close the connection
            if (_connection != null)
                _connection.Close();
        }
    }
}

AsyncPictureGridView.aspx:

<%@ Page Language="C#" MasterPageFile="~/Site.master" CodeFile="AsyncPictureGridView.aspx.cs" Inherits="AsyncPictureGridView" Title="Asynchronous ASP.NET" %>

<asp:Content ID="Content1" ContentPlaceHolderID="Title" Runat="server">
Asynchronous Image Retrieval
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="Description" Runat="server">
This page demonstrates how to display database images in GridViews using an asynchronous HTTP handler
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="Main" Runat="server">
    <asp:SqlDataSource ID="SqlDataSource1" Runat="server" SelectCommand="SELECT [EmployeeID], [LastName], [FirstName], [Photo] FROM [Employees]"
        ConnectionString="<%$ ConnectionStrings:Northwind %>">
    </asp:SqlDataSource>
    <asp:GridView ID="GridView1" Runat="server" DataSourceID="SqlDataSource1" DataKeyNames="EmployeeID"
        AutoGenerateColumns="False" BorderWidth="2px" BackColor="White" GridLines="None"
        CellPadding="3" CellSpacing="1" BorderStyle="Ridge" BorderColor="White" Width="100%">
        <FooterStyle ForeColor="Black" BackColor="#C6C3C6"></FooterStyle>
        <PagerStyle ForeColor="Black" HorizontalAlign="Right" BackColor="#C6C3C6"></PagerStyle>
        <HeaderStyle ForeColor="#E7E7FF" Font-Bold="True" BackColor="#4A3C8C"></HeaderStyle>
        <Columns>
            <asp:TemplateField HeaderText="Photo">
                <ItemStyle Width="1px"></ItemStyle>
                <ItemTemplate>
                    <img src='<%# "AsyncNorthwindImageGrabber.ashx?ID=" + Eval ("EmployeeID") + "&ConnectionString=AsyncNorthwind" %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField ReadOnly="True" HeaderText="Employee ID" InsertVisible="False" DataField="EmployeeID"
                SortExpression="EmployeeID">
                <ItemStyle HorizontalAlign="Center"></ItemStyle>
            </asp:BoundField>
            <asp:BoundField HeaderText="Last Name" DataField="LastName" SortExpression="LastName"></asp:BoundField>
            <asp:BoundField HeaderText="First Name" DataField="FirstName" SortExpression="FirstName"></asp:BoundField>
        </Columns>
        <SelectedRowStyle ForeColor="White" Font-Bold="True" BackColor="#9471DE"></SelectedRowStyle>
        <RowStyle ForeColor="Black" BackColor="#DEDFDE"></RowStyle>
    </asp:GridView>
</asp:Content>

關鍵點:<img src='<%# "AsyncNorthwindImageGrabber.ashx?ID=" + Eval ("EmployeeID") + "&ConnectionString=AsyncNorthwind" %>' />

 


發佈了22 篇原創文章 · 獲贊 3 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章