本文整理汇总了C#中System.Web.IHttpHandlerFactory.GetHandler方法的典型用法代码示例。如果您正苦于以下问题:C# IHttpHandlerFactory.GetHandler方法的具体用法?C# IHttpHandlerFactory.GetHandler怎么用?C# IHttpHandlerFactory.GetHandler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。
在下文中一共展示了IHttpHandlerFactory.GetHandler方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetHandler
// 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>
*/