I have a custom item resolver where I am trying to set the context item based on the path in the url (this is for a re-written url). I've patched this in after Sitecores ItemResolver and it runs fine. However when my page loads the context item doesn't seem to be set correctly when I attached the debugger and none of my renderings get the correct item. It looks similar to this:
public class CustomUrlResolver : HttpRequestProcessor
{
public override void Process(HttpRequestArgs args)
{
Assert.ArgumentNotNull(args, "args");
/*In Case Sitecore has mapped the item, do not do anything and simply return*/
if (Context.Item != null || Context.Database == null || args.Url.ItemPath.Length == 0) return;
/*If not, Check for item based on the FilePath*/
Item contextItem = CustomUrlManager.GetItemByFilePath(args.Url.FilePath);
if (contextItem != null) Context.Item = contextItem;
}
}
As a test I tried to see if I could set the context item in one of my controller renderings and I can force the context item like so:
Item contextItem = CustomUrlManager.GetItemByFilePath("/a-path/my-item", Context.Database);
if (contextItem != null)
{
Context.Item = contextItem;
rc.RenderingContext.Current.Rendering.Item = contextItem;
rc.RenderingContext.Current.ContextItem = contextItem;
rc.RenderingContext.Current.PageContext.Item = contextItem;
}
Other components further down the page then pick up the correct context item However it doesn't seem to work correctly from a httpRequestBegin Pipeline.
I've tried moving when my pipeline executes and also removing any other custom Pipelines we have but can't seem to get the context item to be set correctly.
I am not sure if it's because we are using MVC and Glass and there is something I'm missing here but any ideas would be welcome?
Other info: Sitecore 8.1 update 2, MVC, Glass, url rewrite module: https://marketplace.sitecore.net/Modules/Url_Rewrite.aspx
Sitecore.Context.Item
.