IHttpHandlerFactory.GetHandler Method
Returns an instance of a class that implements the IHttpHandler interface.
Namespace: System.Web
Assembly: System.Web (in System.Web.dll)
IHttpHandler GetHandler( HttpContext context, string requestType, string url, string pathTranslated )
Parameters
- context
- Type: System.Web.HttpContext
An instance of the HttpContext class that provides references to intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.
- requestType
- Type: System.String
The HTTP data transfer method (GET or POST) that the client uses.
- url
- Type: System.String
The RawUrl of the requested resource.
- pathTranslated
- Type: System.String
The PhysicalApplicationPath to the requested resource.
The following code example demonstrates how to create custom handler objects in response to a client request. The example has two parts:
A handler factory class.
A Web.config file excerpt.
The first part of the example shows how to create custom handler objects in response to a client request for a page named either abc.aspx or xyz.aspx. The handler factory class named hwf creates the appropriate handler object depending on the page requested.
// 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> */
The second part of the example shows a Web.config file excerpt. To use the above handler factory, add the following lines to the 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>
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.