DEV Community

Kenneth McAndrew
Kenneth McAndrew

Posted on

Enabling Sitecore Context in HTTP Handlers

A project I'm working on is to perform a migration from a custom ColdFusion system to Sitecore. The client routes all their links through an index.cfm file with an ID parameter that has a formulation to get a SQL primary key. So I need to intercept calls to index.cfm and route them appropriately. Fortunately, there's a way to do this in the web.config using the handlers:

    <system.webServer>
        <handlers>
            <add verb="*" path="index.cfm" type="Foundation.Core.Handlers.LegacyItemResolver, Foundation.Core" name="Foundation.Core.Handlers.LegacyItemResolver" xdt:Locator="Match(path)" xdt:Transform="InsertIfMissing" />
        </handlers>
    </system.webServer>
Enter fullscreen mode Exit fullscreen mode

One catch with this, however...if you want to do anything with the Sitecore context, you'll find it's null. Basically, we're too early in the process to get the context. Yet if you look in the handlers, you'll see a MediaRequestHandler that, if you use dotPeek to check out the reference, does use the Sitecore context. So how did that work? Well, I had to ask around...

1

I'm trying to create a handler to intercept index.cfm files, as we're migrating from an old ColdFusion system to Sitecore. I've designated the handler like this:

<system.webServer>
    <handlers>
        <add verb="*" path="index.cfm" type="Foundation.Core.Pipelines.LegacyItemResolver, Foundation.Core" name="Foundation.Core.Pipelines.LegacyItemResolver" xdt:Locator="Match(path)" xdt:Transform="InsertIfMissing" />
    </handlers>
</system.webServer>

Then my class definition is this, working off the modeling I…

To summarize the answer from JammyKam, I had to add a custom handler entry in a Sitecore patch file:

<configuration xmlns:set="http://www.sitecore.net/xmlconfig/set/">
    <sitecore>
        <customHandlers>
            <handler trigger="index.cfm" handler="cfm_redirect.ashx"/>
        </customHandlers>
    </sitecore>
</configuration>
Enter fullscreen mode Exit fullscreen mode

Then in the web.config entry, instead of the path attribute being set to index.cfm, set it to cfm_redirect.ashx.

Now in my LegacyItemResolver, this code will display the appropriate data:

namespace Foundation.Core.Handlers {
    public class LegacyItemResolver : IHttpHandler, System.Web.SessionState.IRequiresSessionState {
        public void ProcessRequest(HttpContext context) {
            string database = Sitecore.Context.Database.Name;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)