ASP.NET 基础结构HTTP 处理程序和HTTP 模块

    技术2025-02-05  18

    ASP.NET 提供了一个您可用于创建 Web 应用程序的框架。此框架包括了一些服务,如状态管理、HTTP 处理程序、HTTP 模块和 ASP.NET 路由。

     

    HTTP 处理程序和HTTP 模块

    ASP.NET HTTP 处理程序是响应对 ASP.NET Web 应用程序的请求而运行的过程(通常称为"终结点")。最常用的处理程序是处理 .aspx 文件的 ASP.NET 页处理程序。用户请求 .aspx 文件时,页通过页处理程序来处理请求。您可以创建自己的 HTTP 处理程序将自定义输出呈现给浏览器。

    自定义 HTTP 处理程序通常具有以下用途:

    RSS 若要为网站创建 RSS 源,可以创建一个可发出 RSS 格式 XML 的处理程序。然后可以将文件扩展名(如 .rss)绑定到此自定义处理程序。当用户向站点发送以 .rss 结尾的请求时,ASP.NET 将调用您的处理程序来处理请求。

    也即,如果你网站提供了一种如abc.luminji(即以luminji为后缀名)的文件格式,你需要提供一个自定义HTTP处理程序来处理这类文件的请求。

    图像服务器 如果希望 Web 应用程序能够提供不同大小的图像,可以编写一个自定义处理程序来调整图像大小,然后将调整后的图像作为处理程序的响应发送给用户。

    HTTP 模块是一个在每次针对应用程序发出请求时调用的程序集。HTTP 模块作为 ASP.NET 请求管道的一部分调用,它们能够在整个请求过程中访问生命周期事件。HTTP 模块使您可以检查传入和传出的请求并根据该请求进行操作。

    HTTP 模块通常具有以下用途:

    安全 因为您可以检查传入的请求,所以 HTTP 模块可以在调用请求页、XML Web services 或处理程序之前执行自定义的身份验证或其他安全检查。在以集成模式运行的 Internet 信息服务 (IIS) 7.0 中,可以将 Forms 身份验证扩展到应用程序中的所有内容类型。 统计信息和日志记录 因为 HTTP 模块是在每次请求时调用的,所以,您可以将请求统计信息和日志信息收集到一个集中的模块中,而不是收集到各页中。 自定义的页眉或页脚 因为您可以修改传出响应,所以可以在每一个页面或 XML Web services 响应中插入内容,如自定义的标头信息。

    HTTP IHttpHandler IHttpAsyncHandler 接口是开发异步处理程序的起始点。 IHttpModule 接口是开发处理程序和模块的起始点。 处理程序和模块功能包括:

    自定义处理程序和模块源代码可以放在应用程序的 App_Code 文件夹中,也可以在应用程序的 Bin 文件夹中编译和存放。 为在 IIS 6.0 中使用而开发的处理程序和模块经过少许更改或不经更改即可在 IIS 7.0 中使用。有关更多信息,请参见 ASP.NET 应用程序从 IIS 6.0 迁移到 IIS 7.0 模块可以订阅多种请求管道通知。模块可以接收 HttpApplication 对象的事件通知。 IIS 7.0 中,请求管道与 Web 服务器请求管道集成在一起。HTTP 模块可用于对 Web 服务器的任何请求,而不仅仅是 ASP.NET 请求。

     

    同步HTTP 处理程序

    App_Code 目录中,创建一个名为 HelloWorldHandler 的类,并将下面的代码添加到类文件中:

    using System.Web;

    public class HelloWorldHandler : IHttpHandler

    {

    public HelloWorldHandler()

    {

    }

    public void ProcessRequest(HttpContext context)

    {

    HttpRequest Request = context.Request;

    HttpResponse Response = context.Response;

    // This handler is called whenever a file ending

    // in .sample is requested. A file with that extension

    // does not need to exist.

    Response.Write("<html>");

    Response.Write("<body>");

    Response.Write("<h1>Hello from a synchronous custom HTTP handler.</h1>");

    Response.Write("</body>");

    Response.Write("</html>");

    }

    public bool IsReusable

    {

    // To enable pooling, return true here.

    // This keeps the handler in memory.

    get { return false; }

    }

    }

     

    将下面突出显示的元素添加到 Web.config 文件中。

    <httpHandlers>

    <add verb="*" path="*.sample" type="HelloWorldHandler"/>

    </httpHandlers>

     

    异步HTTP 处理程序

    App_Code 目录中,创建一个名为 HelloWorldAsyncHandler的类,并将下面的代码添加到类文件中:

    using System;

    using System.Web;

    using System.Threading;

     

    class HelloWorldAsyncHandler : IHttpAsyncHandler

    {

    public bool IsReusable { get { return false; } }

     

    public HelloWorldAsyncHandler()

    {

    }

    public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)

    {

    context.Response.Write("<p>Begin IsThreadPoolThread is " + Thread.CurrentThread.IsThreadPoolThread + "</p>/r/n");

    AsynchOperation asynch = new AsynchOperation(cb, context, extraData);

    asynch.StartAsyncWork();

    return asynch;

    }

     

    public void EndProcessRequest(IAsyncResult result)

    {

    }

     

    public void ProcessRequest(HttpContext context)

    {

    throw new InvalidOperationException();

    }

    }

     

    class AsynchOperation : IAsyncResult

    {

    private bool _completed;

    private Object _state;

    private AsyncCallback _callback;

    private HttpContext _context;

     

    bool IAsyncResult.IsCompleted { get { return _completed; } }

    WaitHandle IAsyncResult.AsyncWaitHandle { get { return null; } }

    Object IAsyncResult.AsyncState { get { return _state; } }

    bool IAsyncResult.CompletedSynchronously { get { return false; } }

     

    public AsynchOperation(AsyncCallback callback, HttpContext context, Object state)

    {

    _callback = callback;

    _context = context;

    _state = state;

    _completed = false;

    }

     

    public void StartAsyncWork()

    {

    ThreadPool.QueueUserWorkItem(new WaitCallback(StartAsyncTask), null);

    }

     

    private void StartAsyncTask(Object workItemState)

    {

     

    _context.Response.Write("<p>Completion IsThreadPoolThread is " + Thread.CurrentThread.IsThreadPoolThread + "</p>/r/n");

     

    _context.Response.Write("Hello World from Async Handler!");

    _completed = true;

    _callback(this);

    }

    }

     

    将下面突出显示的元素添加到 Web.config 文件中。

    <httpHandlers>

    <add verb="*" path="*.SampleAsync" type="HelloWorldAsyncHandler"/>

    </httpHandlers>

     

    创建自定义HTTP 模块

    App_Code 目录中,创建一个名为 HelloWorldModule.cs(对于 C#)的类文件。

    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;

    string filePath = context.Request.FilePath;

    string fileExtension =

    VirtualPathUtility.GetExtension(filePath);

    if (fileExtension.Equals(".aspx"))

    {

    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;

    string filePath = context.Request.FilePath;

    string fileExtension =

    VirtualPathUtility.GetExtension(filePath);

    if (fileExtension.Equals(".aspx"))

    {

    context.Response.Write("<hr><h1><font color=red>" +

    "HelloWorldModule: End of Request</font></h1>");

    }

    }

     

    public void Dispose() { }

    }

     

    将下面突出显示的元素添加到 Web.config 文件中。

    <httpModules>

    <add name="HelloWorldModule" type="HelloWorldModule"/>

    </httpModules>

     

    转自:http://www.cnblogs.com/luminji/archive/2010/11/24/1886286.html

    最新回复(0)