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 see for the MediaRequestHandler in dotPeek:

namespace Foundation.Core.Pipelines 
{
    public class LegacyItemResolver : IHttpHandler, System.Web.SessionState.IRequiresSessionState {
        public void ProcessRequest(HttpContext context) 
        {
            SiteContext site = Sitecore.Context.Site;
        }

        public bool IsReusable => true;
    }
}

The call fires in the debugger, but the Sitecore.Context comes back as null. I added the session state inherit because someone suggested it would enable the Sitecore.Context, but that's not happening. I figure I need to use the handler via web.config to register the index.cfm name/type in IIS, but I need to access the Sitecore context so I can take an ID parameter and do a search for the legacy ID and redirect the user to the correct URL in Sitecore.

What's the best way to get access to the Sitecore context at this point?

3
  • Why do you need a Sitecore.Context, to get hold of an item (presumably) by ID? Just Facory.GetDatabase("master").GetItem(yourId) or whatever. You have to realise, a Sitecore Context is built (in the httpRequestBegin) pipeline only when an actual Sitecore request is executing. Your handler comes way before that. Aug 20, 2019 at 23:10
  • @MarkCassidy I was trying to use like Context.Database, for one. I'm curious how the MediaRequestHandler works then, since it does use Sitecore.Context but is declared in the same manner in the web.config. Aug 20, 2019 at 23:42
  • I think I have a hack for the database detect using the roles key: string scRoles = ConfigurationManager.AppSettings["role:define"]; string database = scRoles.Contains("ContentDelivery") ? "web" : "master"; Aug 20, 2019 at 23:45

2 Answers 2

6

The reason there is no Sitecore Context available is because the handler you have added is an IIS Handler, and as mentioned in the comments, runs before any Sitecore Context has been resolved.

You also seem to have found an alternative solution, but to create a handler which has access to Sitecore Context you need to register a <customHandler> like so:

<sitecore>
  <customHandlers>
    <handler trigger="index.cfm" handler="cfm_redirect.ashx"/>
  </customHandlers>
</sitecore>

Then add a handler in web.config, ensuring that the path attribute matches the handler attribute you set above:

<system.webServer>
  <handlers>
    <add verb="*" path="cfm_redirect.ashx" type="Foundation.Core.Pipelines.LegacyItemResolver, Foundation.Core" name="Foundation.Core.Pipelines.LegacyItemResolver" />
  </handlers>
</system.webServer>

You can then go ahead and implement the IHttpHandler as you have already done. This is similar to how the media, icon and various other handlers are configured in Sitecore.

1
  • 1
    thanks much! That worked perfectly, and should address your comment to my version of this. Since this gets the Sitecore context in play and that's what I originally wanted, this is the best answer here. Aug 22, 2019 at 15:29
0

I had been using Sitecore.Context to do some checks for sites, but since only certain links will call this old index.cfm, based on what Mark commented, I didn't need that. That left me with wanting to determine which database to use, but since Context.Database wouldn't work, I used this "hack" way to determine which to use:

ConfigurationManager.AppSettings["role:define"];
string database = scRoles.Contains("ContentDelivery") ? "web" : "master";

Since I really only use the web database in CD mode, this will work for this case. After that, using the Sitecore search APIs to find the legacy ID field and look things up based on that.

2
  • 4
    If you're gonna do that, why not just set it as a config value and just read it straight as a Sitecore setting instead of putting the logic in code? <setting name="ContentDatabase" value="master" /> <setting name="ContentDatabase" set:value="web" role:require="ContentDelivery" />
    – jammykam
    Aug 21, 2019 at 0:53
  • 1
    I agree with @jammykam here, it will give you more flexibility in case you have a preview environment with preview database. Aug 21, 2019 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.