יום שבת, 29 בינואר 2011

implementing IHttpHandler of Your Own

Adding an HttpHandler to your web.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.web>
    <httpHandlers>
      <add verb="*" path="*.jpg" type="MyClass, MyDll" />
    </httpHandlers>
  </system.web>
</configuration>

To write an HttpHandler, you create a class that implements the IHttpHandler interface. All of the handlers
 do this. You might recall from  "Classes: The Code Behind the Objects," that an interface is used to ensure that a well-known means of communicating with some other code is available. For ASP.NET to communicate with an HttpHandler, it must have a couple members defined by the interface. Listing 8.2 shows the basic interface.

Listing 8.2. A class implementing IHttpHandler
C#
public class MyHttpHandler : IHttpHandler
{
  public void ProcessRequest(HttpContext context)
  {
    // do something here
  }

  public bool IsReusable
  {
    get { return true; }
  }
}
The ProcessRequest() method is where we do work in response to the request. ASP.NET passes in a reference to the HttpContext object associated with the request. You'll notice in Visual Studio that as soon as you type a period following the context parameter, Intellisense will show you properties that correspond to all the familiar objects you might use in a page

אין תגובות:

הוסף רשומת תגובה