Adding generic handlers to a Sitecore project

Bernard Lim
Sitecore Series
Published in
2 min readAug 4, 2018

Say for example we have the following requirements in a Sitecore project:

  • Make AJAX calls from a Sitecore webpage
  • Process AJAX request, then call external API accordingly
  • Process response from API and return result back to Sitecore webpage.

There are a couple of ways to accomplish this:

  • Webpage (ASPX)
    I have seen legacy projects using a webpage (ASPX) to do so but since there is no UI required, this is not necessary as there will be extra overhead from the page lifecycle events.
  • MVC Controllers
    The most common technique will be to use MVC controllers but this can only be done if your project is already in MVC.
  • Web API
    Using a Web API can also be another clean alternative, but what if you do not need that many files / new project for this? Just a simple file will be your preferred method.
  • Generic Handlers (ASHX)
    In my opinion, this is the quickest no-frills way to accomplish the requirement.

In order to add custom generic handler to a Sitecore project:
(Assume sub-site is called ‘mysubsite’)

  1. Create an item of ‘Generic Handler’ under your respective Sitecore project.
  2. Declare your handler under <customHandlers> section in your respective ‘Include_xxxx.config’ file.

3. Declare your handler also in the <handlers> section in the web.config file.

4. Remember to also implement ‘IRequiresSessionState’ if you require Session information.

Photo by Julien Lanoy on Unsplash

--

--

Bernard Lim
Sitecore Series

@thebernardlim. Code. Sitecore. Fitness. Food. Random