定义类工厂为创建新的IHttpHandler对象而必须实现的协定。
命名空间:System.Web
程序集:System.Web.dll
语法:public interface IHttpHandlerFactory
方法:
GetHandler 返回实现IHttpHandler接口的类的实例
ReleaseHandler 使工厂可以重用现有的处理程序实例
IHttpHandlerFactory.GetHandler方法
返回实现IHttpHandler接口的类的实例。
语法:
IHttpHandler GetHandler(HttpContext context,string requestType,string url,string pathTranslated)
参数:
context HttpContext类的实例,它提供对用于为Http请求提供服务的内部服务对象(如Requst、Response、Session和Server)的引用
requestType 客户端使用的Http数据传输方法(GET或POST)
url 所请求的资源的RawUrl
pathTranslated 所请求资源的PhysicalApplicationPath
IHttpHandlerFactory.ReleaseHandler方法
语法:
void ReleaseHandler(IHttpHandler handler)
参数:
handler 要重用的IHttpHandler对象
// Name this C# file HandlerFactoryTest.cs and compile it with the
// command line: csc /t:Library /r:System.Web.dll HandlerFactoryTest.cs.
// Copy HandlerFactoryTest.dll to your \bin directory.
namespace test
{
using System;
using System.Web;
// Factory class that creates a handler object based on a request
// for either abc.aspx or xyz.aspx as specified in the Web.config file.
public class MyFactory : IHttpHandlerFactory
{
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
public virtual IHttpHandler GetHandler(HttpContext context,
String requestType,
String url,
String pathTranslated)
{
String fname = url.Substring(url.LastIndexOf('/')+1);
String cname = fname.Substring(0, fname.IndexOf('.'));
String className = "test." + cname;
Object h = null;
// Try to create the handler object.
try
{
// Create the handler by calling class abc or class xyz.
h = Activator.CreateInstance(Type.GetType(className));
}
catch(Exception e)
{
throw new HttpException("Factory couldn't create instance " +
"of type " + className, e);
}
return (IHttpHandler)h;
}
// This is a must override method.
public virtual void ReleaseHandler(IHttpHandler handler)
{
}
}
// Class definition for abc.aspx handler.
public class abc : IHttpHandler
{
public virtual void ProcessRequest(HttpContext context)
{
context.Response.Write("<html><body>");
context.Response.Write("<p>ABC Handler</p>\n");
context.Response.Write("</body></html>");
}
public virtual bool IsReusable
{
get { return true; }
}
}
// Class definition for xyz.aspx handler.
public class xyz : IHttpHandler
{
public virtual void ProcessRequest(HttpContext context)
{
context.Response.Write("<html><body>");
context.Response.Write("<p>XYZ Handler</p>\n");
context.Response.Write("</body></html>");
}
public virtual bool IsReusable
{
get { return true; }
}
}
}
/*
______________________________________________________________
To use the handler factory, use the following lines in a
Web.config file.
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="abc.aspx" type="test.MyFactory,HandlerFactoryTest" />
<add verb="*" path="xyz.aspx" type="test.MyFactory,HandlerFactoryTest" />
</httpHandlers>
</system.web>
</configuration>
*/
// command line: csc /t:Library /r:System.Web.dll HandlerFactoryTest.cs.
// Copy HandlerFactoryTest.dll to your \bin directory.
namespace test
{
using System;
using System.Web;
// Factory class that creates a handler object based on a request
// for either abc.aspx or xyz.aspx as specified in the Web.config file.
public class MyFactory : IHttpHandlerFactory
{
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
public virtual IHttpHandler GetHandler(HttpContext context,
String requestType,
String url,
String pathTranslated)
{
String fname = url.Substring(url.LastIndexOf('/')+1);
String cname = fname.Substring(0, fname.IndexOf('.'));
String className = "test." + cname;
Object h = null;
// Try to create the handler object.
try
{
// Create the handler by calling class abc or class xyz.
h = Activator.CreateInstance(Type.GetType(className));
}
catch(Exception e)
{
throw new HttpException("Factory couldn't create instance " +
"of type " + className, e);
}
return (IHttpHandler)h;
}
// This is a must override method.
public virtual void ReleaseHandler(IHttpHandler handler)
{
}
}
// Class definition for abc.aspx handler.
public class abc : IHttpHandler
{
public virtual void ProcessRequest(HttpContext context)
{
context.Response.Write("<html><body>");
context.Response.Write("<p>ABC Handler</p>\n");
context.Response.Write("</body></html>");
}
public virtual bool IsReusable
{
get { return true; }
}
}
// Class definition for xyz.aspx handler.
public class xyz : IHttpHandler
{
public virtual void ProcessRequest(HttpContext context)
{
context.Response.Write("<html><body>");
context.Response.Write("<p>XYZ Handler</p>\n");
context.Response.Write("</body></html>");
}
public virtual bool IsReusable
{
get { return true; }
}
}
}
/*
______________________________________________________________
To use the handler factory, use the following lines in a
Web.config file.
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="abc.aspx" type="test.MyFactory,HandlerFactoryTest" />
<add verb="*" path="xyz.aspx" type="test.MyFactory,HandlerFactoryTest" />
</httpHandlers>
</system.web>
</configuration>
*/