1.IHttpHandler介面
定義了實現一個HttpRequest的處理所必須實現的一些系統約定方法。
public interface IHttpHandler
{
//其他Request是否可以使用IHttpHandler
bool IsReusable { get; }
//處理HttpRequest
void ProcessRequest(HttpContext context);
}
NET為ASP.NET提供了很多系統預設HttpHandler類,用來適應不同類型的HttpRequest
比如aspx,在machine.config中是這樣定義的:
<add verb="*" path="*.aspx" type="System.Web.UI.PageHandlerFactory"/>
說明遇到aspx的Request,ASP.Net會將其交給System.Web.UI.PageHandlerFactory的HttpHandler類來處理
如果自己定義了新的HttpHandler,而且在Web.config中指定,則系統只會使用這個新的HttpHandler,而不再使用原先指定的
2.HttpHandler實現了IHttpHandler介面
一個aspx頁面在HttpHandler容器中的ProcessRequest方法才被系統真正的處理解析——即交給PageHandlerFactory處理,該工廠負責提供一個HttpHandler容器,由其處理HttpRequest
3.如果要在HttpHandler容器中使用Session,必須要實現IRequiresSessionState介面——這隻是一個空介面,一個標記
using System;
using System.Web;
using System.Web.SessionState;
namespace MyNamespace
{
public class MyHandler:IHttpHandler,IRequiresSessionState
{
public MyHandler() {}
public bool IsReusable
{
get
{
return true;
}
}
public void ProcessRequest(HttpContext context)
{
HttpResponse response = context.Response;
HttpRequest request = context.Request;
HttpSessionState Session = context.Session;
Session["test"] = "hi";
response.Write("<b>Hello world!</b>");
response.Write(Session["test"]);
}
}
}
同時,還要在Web.config中加上聲明: <httpHandlers>
<add verb="*" path="*" type="MyNamespace.MyHandler,MyNamespace"></add>
</httpHandlers>
4.IHttpHandlerFactory
待續。。。