How to implement IHttpHandler in a ContentHandler

From Sense/Net Wiki
Jump to: navigation, search
  • 100%
  • 6.0
  • Enterprise
  • Community
  • Planned


There is a way to generate custom HTTP response without using the Sense/Net view engine or any other ASP.NET rendering. For this purpose you can implement a Content Handler as an IHttpHandler. For example you could create a Content Type for sitemap configurations and when the sitemap configuration Content is requested in a browser the ContentHandler for the type returns the sitemap with the given configuration in XML format - simply using the common IHttpHandler interface. This next tutorial shows you how it's done.


1. Create a new class

2. Implement content handler

Name your new class file MySitemapHandler and paste the following code into the file's editor window:

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using SenseNet.ContentRepository;
using SenseNet.ContentRepository.Schema;
using SenseNet.ContentRepository.Storage;
namespace MySitemapHandler
    public class MySitemapHandler : GenericContent
        // =================================================================================================== Constructors
        // for initialize a new MyContentHandler instance
        public MySitemapHandler(Node parent) : this(parent, "MySitemap") { }
        // for initialize a new MyContentHandler inherited instance
        public MySitemapHandler(Node parent, string nodeTypeName) : base(parent, nodeTypeName) { }
        // for initialize instance in the loading operation
        protected MySitemapHandler(NodeToken nt) : base(nt) { }

This is the minimal content handler. This content handler won't store anything, won't have any setting and won't do any extra repository function, so this is good enough for a starting point.

3. Implement the IHttpHandler interface

Extend the class declaration line with the IHttpHandler declaration:

    public class MySitemapHandler : GenericContent, IHttpHandler

If the interface is unknown, reference the System.Web.dll to the project and add the System.Web namespace to the "using" block.

using System.Web;

To add the implementation skeleton move the caret to the IHttpHandler word, press Alt+Shift+F10 and select Implement interface 'IHttpHandler'. After that you get some bonus code:

        public bool IsReusable
            get { throw new NotImplementedException(); }
        public void ProcessRequest(HttpContext context)
            throw new NotImplementedException();

Return with true value from the IsReusable property because this handler will be stateless and thread safe.

        public bool IsReusable
            get { return true; }

4. Implement the ProcessRequest method

Implement the sitemap generator code that returns (for example) an XmlDocument (description of sitemap generator code is not a task of this article):

        private XmlDocument GenerateSitemapFromPath(string path)
            // generates the sitemap XML dom by the given path
            var doc = new XmlDocument();
            // TODO: implement xml generation
            return doc;

At the end you must integrate the sitemap generator into your content Handler.

        public void ProcessRequest(HttpContext context)
            // get sitemap DOM object
            var xml = GenerateSitemapFromPath(this.Path);
            // write XML to output
            context.Response.ContentType = "application/xml";
            // set ok
            context.Response.StatusCode = 200;

5. Test the content handler

Create your new MySitemap content type if you have not yet done so. Handler attribute should point to the create content handler:

<?xml version="1.0" encoding="utf-8"?>
<ContentType name="MySitemap" parentType="GenericContent" handler="MySitemapHandler.MySitemapHandler" xmlns="">

Now in Content Explorer go to /Root/Sites/Default_Site and create a new MySitemap with the name sitemap (don't forget to add your MySitemap type to the allowed child types of the site). After that open a new window and request http://localhost/sitemap This link works only if you have a live demo install on your localhost! (Don't forget! Some feature needs authentication and/or admin rights!). The generated xml output should appear in your browser.