Adding Sitecore.Context to Custom Controllers

January 8, 2014

If you are building out a Sitecore MVC project or are just integrating Web API into your web forms project, you are going to want to expose the Sitecore Context to your controllers. An easy way to do this is to add a Route Pipeline in your httpRequestBegin section that will expose the Context. To do this, we will need to set up a patch config and a custom class that will actually expose the Sitecore Context.

Patch Config

<configuration xmlns:patch="" xmlns:x="">
        <processor x:before="*[7]" type="Your.Framework.Pipelines.RoutePipeline, Your.Framework" />

Your.Framework.Pipelines.RoutePipeline - Custom Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Routing;
using Sitecore.Pipelines.HttpRequest;

namespace Your.Framework.Pipelines
    public class RoutePipeline : HttpRequestProcessor
         public override void Process(HttpRequestArgs args)
             RouteData routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(args.Context));

             if (routeData != null)

Once you link these up the Sitecore Context will be exposed. You can confirm that the pipeline is being added to the config by going to /sitecore/admin/showConfig.aspx.

Tags: MVC,Sitecore,C#

Back to Posts