IHttpHandler工厂
class TestHandlerFactory : IHttpHandlerFactory
{
public IHttpHandler GetHandler
(HttpContext context, string requestType, string url, string pathTranslated)
{
string fname = url.Substring(url.IndexOf('/') + 1);
while (fname.IndexOf('/') != -1)
fname = fname.Substring(fname.IndexOf('/') + 1);
string cname = fname.Substring(0, fname.IndexOf('.'));
string className ="";
className = "ClassLibrary831.CodeHandler";
object h = null;
try
{
//h = new TestHandler();
h = Activator.CreateInstance(Type.GetType(className));
}
catch (Exception e)
{
throw new HttpException("工厂不能为类型" + cname + "创建实例。", e);
}
return (IHttpHandler)h;
}
public void ReleaseHandler(IHttpHandler handler)
{
}
}(车延禄)
|
配置文件
<httpHandlers>
<add verb="*" path
="default21.aspx,default22.aspx" type
="ClassLibrary831.TestHandlerFactory,
ClassLibrary831"></add>
</httpHandlers>
|
这样TestHandlerFactory就会根据请求的不同页面执行不同的HttpHandler处理程序了。
HttpHandler使用会话
如果要在处理程序中使用Session,那必须把该HttpHandler实现IRequiresSessionState接口,,IRequiresSessionState接口是个空接口,它没有抽象方法,只是一个标记。此处就不作例子验证了