1。HttpModulehtml
最多見的是使用HttpModule來作頁面權限控制。spring
在新建類庫添加以下代碼:url
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
using
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
namespace
HttpModuleCK
{
public
class
UserControl : IHttpModule
{
public
void
Dispose()
{ }
public
void
Init(HttpApplication context)
{
context.BeginRequest +=
new
System.EventHandler(httpApplication_BeginRequest);
}
public
void
httpApplication_BeginRequest(
object
sender, EventArgs e)
{
HttpApplication httpApplication = (HttpApplication)sender;
string
url = httpApplication.Context.Request.Path;
//作用戶權限控制
HttpContext content = (HttpContext)httpApplication.Context;
//if (httpApplication.Request["userid"] == null)
//{
// content.Response.Write("未提供必需的參數!!");
// content.Response.End();
//}
}
}
}
|
在Web項目中添加引用該類,添加配置信息便可,spa
1
2
3
|
<httpModules>
<add name=
"UserRight"
type=
"HttpModuleCK.UserControl, HttpModuleCK"
/>
</httpModules>
|
2.HttpHandler.net
這個經常使用來作文件訪問控制,圖片防盜鏈等等.code
新建類庫添加以下代碼:htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
using
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
namespace
HttpHandlerCK
{
public
class
docHandle:IHttpHandler
{
public
void
ProcessRequest(HttpContext context)
{
string
FileName = context.Server.MapPath(context.Request.FilePath);
if
(context.Request[
"userid"
] ==
null
)
{
context.Response.Write(
"doc文件沒法訪問!!"
);
context.Response.End();
}
}
public
bool
IsReusable
{
get
{
return
false
;
}
}
}
}
在Web項目的config中配置以下:
|
1
2
3
|
<httpHandlers>
<add verb=
"*"
path=
"*.doc"
type=
"HttpHandlerCK.docHandle, HttpHandlerCK"
/>
</httpHandlers>
|
3.HttpHandlerFactory對象
功能比HttpHandler要更增強大,HttpHandlerFactory是HttpHandler的工廠,經過它來生成不一樣的HttpHandler對象。生命週期
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
using
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
namespace
HandlerFactoryCK
{
public
class
aspxHandle:IHttpHandlerFactory
{
public
IHttpHandler GetHandler(HttpContext context,
string
requestType,
string
url,
string
pathTranslated)
{
PageHandlerFactory factory = (PageHandlerFactory)Activator.CreateInstance(
typeof
(PageHandlerFactory),
true
);
IHttpHandler handler = factory.GetHandler(context, requestType, url, pathTranslated);
//執行一些其它操做
Execute(handler);
return
handler;
}
private
void
Execute(IHttpHandler handler)
{
if
(handler
is
Page)
{
//能夠直接對Page對象進行操做
((Page)handler).PreLoad +=
new
EventHandler(MyHandlerFactory_PreLoad);
}
}
void
MyHandlerFactory_PreLoad(
object
sender, EventArgs e)
{
((Page)sender).Response.Write(
"Copyright @Gspring<br/>"
);
}
public
void
ReleaseHandler(IHttpHandler handler)
{
}
}
}
在Webconfig配置以下:
|
1
2
3
|
<httpHandlers>
<add verb=
"*"
path=
"*.aspx"
type=
"HandlerFactoryCK.aspxHandle, HandlerFactoryCK"
/>
</httpHandlers>
|
三者的區別以下:進程
生命週期中涉及到幾個很是重要的對象:HttpHandler,HttpModule,IHttpHandlerFactory,他們的執行(順序)大體的執行過程是這樣的:
client端發送頁面請求,被IIS的某個進程截獲,它根據申請的頁面後綴(.aspx)不一樣,調用不一樣的頁面處理程序(.asp->asp.dll; .aspx->ISAPI.dll)。而頁面處理程序在處理過程當中,則要經歷HttpModule,HttpHandler的處理:前者HttpModule用於頁面處理前和處理後的一些事件的處理,後者HttpHandler進行真正的頁面的處理。
如前所說,HttpModule會在頁面處理前和後對頁面進行處理,因此它不會影響真正的頁面請求。一般用在給每一個頁面的頭部或者尾部添加一些信息(如版權聲明)等。曾經見過一些免費的空間,咱們的頁面上傳上去後,瀏覽的時候發現,在每一個頁面的頭部和尾部多了不少小廣告....若是理解了HttpModule的原理,要作這個就不是很難了~
IHttpModule與IHttpHandler的區別整理 1.前後次序.先IHttpModule,後IHttpHandler. 注:Module要看你響應了哪一個事件,一些事件是在Handler以前運行的,一些是在Handler以後運行的 2.對請求的處理上: IHttpModule是屬於大小通吃類型,不管客戶端請求的是什麼文件,都會調用到它;例如aspx,rar,html的請求. IHttpHandler則屬於挑食類型,只有ASP.net註冊過的文件類型(例如aspx,asmx等等)纔會輪到調用它. 3.IHttpHandler按照你的請求生成響應的內容,IHttpModule對請求進行預處理,如驗證、修改、過濾等等,同時也能夠對響應進行處理