简单介绍一下<httphander>与<httpModule>
httphander 重定向URL
WebConfig中的配置
<httpHandlers>
<add verb="GET" path="bb.karen" type="BLL.HanderTest,BLL"/>
</httpHandlers>
其中BLL.HanderTest为类,BLL为所属DLL
实现IHttpHandler接口的类
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace BLL
{
class HanderTest : IHttpHandler
{
public HanderTest()
{
}
public void ProcessRequest(System.Web.HttpContext context)
{
System.Web.HttpResponse obj = context.Response;
obj.Write("<html><body><h1>this is a httphandler test!");
obj.Write("</body></html>");
}
public bool IsReusable
{
get
{
return true;
}
}
}
}
当在页面中输入http:/…/../bb.karen 时将显示 “this is a httphandler test!”,以此达到重定向的目的。
httpModule 实现权限验证
WebConfig配置
<configuration>
<system.web>
<httpModules>
<add name=" MyModule " type=" MyModule, MyModule" />
</httpModules>
</system.web>
</configuration>
类中实现
using System;
using System.Web;
namespace MyModule
{
public class MyModule : IHttpModule
{
public void Init(HttpApplication application)
{
application. AcquireRequestState += (new
EventHandler(this.Application_AcquireRequestState));
}
private void Application_AcquireRequestState (Object source, EventArgs e)
{
HttpApplication Application = (HttpApplication)source;
User user=Application.Context.Sesseion["User"] //获取User
string url=Application.Context.Request.Path;
//获取客户访问的页面
Module module= //根据url得到所在的模块
If(!RightChecker.HasRight(user,module))
Application.Context.Server.Transfer("ErrorPage.aspx");
//如果没有权限,引导到错误处理的页面
}
public void Dispose()
{
}
}
}