Creating a customHandler in Sitecore

Sometimes you would like all requests to a specific path and below to be handled by a specific generic handler. For example in the same manner as the mediaHandler works in Sitecore.

Setting up a customHandler in Sitecore

Configuring a normal httpHandler in .NET is easy, you simply add entries in the web.config depending on your version of IIS.

UnderĀ /configuration/system.webServer/handlers (for IIS 7 and above)

and the same underĀ /configuration/system.web/httpHandlers (for older versions of IIS)

To make the handler react on a specific path simply add a path attribute in the configuration and all requests which contains this path will be handled by this specific generic handler.

An example could be:

Where the ExampleHandler class will handle all requests for any path postfixed with .example

Sitecore made a small addition to this configuration enabling the handlers to be resolved by Sitecore.

To make this work do not add a path attribute to the handlers as you normally would in ASP.NET instead create a new config entry as follows under /configuration/sitecore/customHandlers/ see the last entry in the below example:

Now the handler we defined before called examplehandler.ashx will catch all requests to the relative path /-/examplehandler/ as start path and handle the request with the specified handler.

Now to implementing the handler.

Remember to implement the IRequiresSessionState interface if the handler should use the ASP.NET session.

That is it. In a soon to come blog post I will show how this concept can be used to deliver CSS dynamically generated from items in Sitecore using LESS.

I dare you to like this post..

Anders Laub

Anders Laub Christoffersen

Anders has been working with Sitecore for over a decade and has in this time been the lead developer and architect on several large scale enterprise solutions all around the world. Anders has been nominated a Sitecore Technical MVP three years in a row for 2014, 2015 and 2016. Anders is now working as a Sr. Solutions Architect at Sitecore in Copenhagen.

5 thoughts on “Creating a customHandler in Sitecore

  1. Hi .. Your post is very informative… I had a doubt that what is the [Assembly] which we should refer for a handler ?
    for my case i didn’t refer any assembly but i couldn’t connect to the handler that i created. please advice.

    • Hi,

      Sorry for the slow reply. I’ve been off my blog for some time now.

      [Assembly] refers to the name of the dll in which the code resides.

      –Anders

  2. for more information,
    1. I have created a handler named “EventsHandler.ashx” under the root of my website.
    2. under the system.webServer/handlers and system.web/httpHandlers of sitecore web.config file.
    3. Added under CustomHandler tag.
    (Note : i used trigger=”~/EventsHandler/” instead of /-/EventsHandler )

    And im calling this handler in the user control.
    But unable to connect to handler… Please advice me whether we need to make any changes to call that handler for a usercontrol (ascx).

    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*
Website