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对请求进行预处理,如验证、修改、过滤等等,同时也能够对响应进行处理