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)
1 |
<add verb="*" path="examplehandler.ashx" type="[NAMESPACE].ExampleHandler, [ASSEMBLY]" name="ExampleHandler" /> |
and the same underĀ /configuration/system.web/httpHandlers (for older versions of IIS)
1 |
<add verb="*" path="examplehandler.ashx" type="[NAMESPACE].ExampleHandler, [ASSEMBLY]" name="ExampleHandler" /> |
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:
1 |
<add verb="*" path="*.example" type="[NAMESPACE].ExampleHandler, [ASSEMBLY]" name="ExampleHandler" /> |
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:
1 2 3 4 5 6 7 8 |
<customHandlers> <handler trigger="~/media/" handler="sitecore_media.ashx" /> <handler trigger="~/api/" handler="sitecore_api.ashx" /> <handler trigger="~/xaml/" handler="sitecore_xaml.ashx" /> <handler trigger="~/icon/" handler="sitecore_icon.ashx" /> <handler trigger="~/feed/" handler="sitecore_feed.ashx" /> <handler trigger="/-/examplehandler/" handler="examplehandler.ashx" /> </customHandlers> |
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.
1 2 3 4 5 6 7 8 9 |
public class ExampleHandler: IHttpHandler, IRequiresSessionState { public void ProcessRequest(HttpContext context) { context.Response.Write("Hello world"); } public bool IsReusable { get; private set; } } |
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 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 was appointed the title of Sitecore Technical MVP in 2014 and has been re-appointed the title every year since then.
- Web |
- More Posts
Thanks, great post!
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
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