home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / QuickStart / aspplus / doc / httphandlers.aspx < prev    next >
Encoding:
Text File  |  2000-06-11  |  5.0 KB  |  150 lines

  1.  
  2. <%@ Register TagPrefix="Acme" TagName="SourceRef" Src="/quickstart/util/SrcRef.aspx"%>
  3.  
  4. <!-- #include virtual="/quickstart/aspplus/include/header.inc" -->
  5.  
  6. <h4>HttpHandlers and Factories</h4>
  7.  
  8. <div class="indent" style="font-family:Verdana; font-size:8pt;">
  9.     <b> <img align="middle" src="/quickstart/images/bullet.gif">  </b>
  10.     <a class="toc2" target="content" href="#overview">Overview</a><br>
  11.     <b> <img align="middle" src="/quickstart/images/bullet.gif">  </b>
  12.     <a class="toc2" target="content" href="#declaring">Configuring HttpHandlers and Factories</a><br>
  13.     <b> <img align="middle" src="/quickstart/images/bullet.gif">  </b>
  14.     <a class="toc2" target="content" href="#customhandler">Creating a Custom HttpHandler</a><br>
  15. </div>
  16.  
  17. <p>
  18. <hr>
  19.  
  20.  
  21. <!--BEGIN SECTION-->
  22. <a name="overview">
  23. <span class="subhead">Overview</span>
  24. <p>
  25.  
  26. ASP+ provides a low-level request/response API that enables developers to use 
  27. NGWS Runtime classes to service incoming HTTP requests. Developers accomplish
  28. this by authoring classes that support the <b>System.Web.IHTTPHandler</b> interface
  29. and implement the <b>ProcessRequest()</b> method.  Handlers are often useful when
  30. the services provided by the high-level Page Framework abstraction are not required 
  31. for processing the http request.  Common uses of handlers include filters and
  32. CGI-like applications, especially those that return binary data.
  33.  
  34. <p>
  35. Each incoming HTTP request received by ASP+ is ultimately processed by a specific
  36. instance of a class implementing <b>IHTTPHandler</b>. <b>IHTTPHandler Factories</b> provide the infrastructure
  37. that handles the actual resolution of URL requests to IHTTPHandler instances. 
  38. ASP+ ships with three standard IHTTPHandler factories - PageFactory, 
  39. RestrictedResourceFactory, and WebServiceFactory.  
  40. Developers can optionally create and register additional factories to support
  41. richer request resolution and activation scenarios.
  42.  
  43. <!--BEGIN SECTION-->
  44. <br>
  45. <a name="declaring">
  46. <br>
  47. <span class="subhead">Configuring HttpHandlers and Factories</span>
  48. <p>
  49.  
  50. Http Handlers and Factories are declared in the ASP+ configuration as part
  51. of a config.web file. ASP+ defines an "httphandlers" configuration section 
  52. where handlers and factories can be added and removed. Settings for 
  53. HttpHandlerFactories are inherited by sub-directories, settings for HttpHandler are NOT
  54. inherited. 
  55. <p>
  56.  
  57. ASP+ for example maps all requests for .aspx files to the <b>PageHandlerFactory</b> class in 
  58. top config.web:
  59.  
  60. <div class="code"><pre>
  61. <httphandlers>
  62.   ...
  63.   <add verb="*" path="*.aspx" type="System.Web.UI.PageHandlerFactory" />
  64.   ...
  65. </httphandlers>
  66. </pre></div>
  67.  
  68. <!--BEGIN SECTION-->
  69. <br>
  70. <a name="customhandler">
  71. <br>
  72. <span class="subhead">Creating a Custom Http Handler</span>
  73. <p>
  74.  
  75. In the following sample a custom <b>HttpHandler</b> is created, which handles
  76. all requests to "SimpleHandler.aspx" in the respective directory.
  77. <p>
  78.  
  79. <Acme:SourceRef 
  80.   RunSample="/quickstart/aspplus/samples/apps/handler/simplehandler.aspx" 
  81.   ViewSource="/quickstart/aspplus/samples/apps/handler.src"
  82.   Icon="/quickstart/aspplus/images/simplehandler.gif"
  83.   Caption="SimpleHandler"
  84.   runat="server" />
  85. <p>
  86.  
  87.  
  88. A custom HTTP handler can be created by implementing the IHttpHandler 
  89. interface, which contains only two methods. By calling <b>IsReusable</b> a 
  90. HTTP factory can query a handler to determine whether the same instance can be used to service multiple requests. The
  91. ProcessRequest method takes an <b>HttpContext</b> instance as a parameter, which
  92. gives it access to the <b>Request</b> and <b>Response</b> intrinsics. 
  93. In this sample, request data is ignored and a constant
  94. string is sent as a response to the client:
  95.  
  96. <div class="code"><xmp>
  97. public class SimpleHandler : IHttpHandler
  98. {
  99.   public void ProcessRequest(HttpContext context)
  100.   {
  101.     context.Response.Write("Hello World!");
  102.   }
  103.  
  104.   public bool IsReusable()
  105.   {
  106.     return true;
  107.   } 
  108. }
  109. </xmp></div>
  110.  
  111. After placing the compiled handler assembly in the application's /bin directory,
  112. the handler class can be specified as a target for requests. In this case all
  113. requests for "SimpleHandler.aspx" will be routed to an instance of the 
  114. SimpleHandler class, which lives in the namespace Acme.SimpleHandler:
  115.  
  116. <div class="code"><xmp>
  117. <httphandlers>
  118.   <add verb="*" path="SimpleHandler.aspx" type="SimpleHandler#Acme.SimpleHandler" />
  119. </httphandlers> 
  120. </xmp></div>
  121.  
  122. <p>
  123.  
  124. <h4>Section Summary</h4>
  125. <ol>
  126. <li>HTTP Handlers and Factories are the backbone of the ASP+ framework.
  127. <li>Factories assign each request to one handler, which processes the request.
  128. <li>Factories and handlers are defined in the config.web file. Settings for factories 
  129. are inherited by sub-directories.
  130. <li>To create a custom handler, implement IHttpHandler and add the class in the 
  131. httphandlers section of the config.web in the directory.
  132. </ol>
  133. <p>
  134.  
  135. <!-- #include virtual="/quickstart/aspplus/include/footer.inc" -->
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.