Customise WCM using a custom HTTP Handler

  • KM507255
  • 20-Sep-2008
  • 22-Jul-2010

Archived Content: This information is no longer maintained and is provided "as is" for your convenience.

Summary

Provides information on how to create custom HTTP handler in WCM.

Reference

Return to WCM Articles page


Overview

WCM uses custom HTTP handlers to render a variety of resources, including image and document library items, site searching and standard WCM pages. WCM also provides a facility for a customer to create their own custom HTTP handler and include it in WCM.

Custom HTTP handlers versus WCM custom fields

WCM custom fields are used for the purpose of injecting snippets of XML into the WCM XML document. For example the HTML editor is a commonly used custom field. On the other hand a custom HTTP handler is used to render an entire HTTP request. For example WCM uses a custom HTTP handler to render each individual page.

It is likely that most advanced WCM customisation can be achieved using a custom field working with the confines of a WCM page. There are times, however when a custom field will not provide enough control of the HTTP response. An example of where a custom HTTP handler may be generating a custom Captcha image from within WCM.

"Hello world!" example

To create a simple custom HTTP handler:

  1. create a class library project in MS Visual Studio,
  2. create a class using the following code,
  3. compile the class,
  4. copy the assembly to both the wcm\bin and wcm\sites\bin folders,
  5. update the user.handlers.config file (see below),
  6. restart IIS.
using System.Web;
using System.Web.SessionState;
namespace Tower.Wcm.Examples
{
 /// <summary>
 /// Write a web page with the text Hello world!
 /// </summary>
 public class HelloWorldHandler : IHttpHandler, IRequiresSessionState
 {
  public HelloWorldHandler()
  {
  }
  public void ProcessRequest( HttpContext context )
  {
   // Change the response headers to output a JPEG image.
   context.Response.Clear();
   context.Response.ContentType = "text/html";
   string content = "<html><head><title>Hello world!</title></head><body><h1>Hello world!</h1></body></html>";
   context.Response.Write( content );
  }
  public bool IsReusable
  {
   get { return true; }
  }
 }
}

Config file

Create a file in the wcm folder and paste the following text into it. Change the text “Tower.Wcm.Examples.HelloWorldHandler” to the full name (name space and class name) of your hello world class and change the text “Tower.Wcm.Examples” to the file name of your assembly. The Root attribute tells WCM what text to use to find this handler, for example if an end user enters the URL http://myserver/mywcmsite/_hw/ they will trigger this handler.

<?xml version="1.0" encoding="utf-8" ?>
<HandlerConfigList xmlns:xsd="
http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <Handlers>
  <Handler Name="HelloWorldHandler" Root="_hw/" Class="Tower.Wcm.Examples.HelloWorldHandler, Tower.Wcm.Examples, Culture=neutral"/>
 </Handlers>
</HandlerConfigList>