November Happy Hour will be moved to Thursday December 5th.

How to get current content from executing context?

Vote:
 

Hi,

I’m currently working on donut hole caching for our project. In order to generate correct cache keys in the ActionFilterAttribute I need to get current content from ControllerContext. I found multiple blog posts and answers that say you can get it like this: filterContext.Controller.ControllerContext.RouteData.Values["currentContent"]. There is even a demo project of donut hole caching for EPiServer that uses this logic: https://github.com/jondjones/JonDJones.Com.EpiServerDonutCaching/blob/master/JonDJones.Com.DonutHoleCaching/EpiServerDonutHelper.cs at line 77. However, the problem is, that I never get anything else in the RouteData besides language, partial, action and controller (please look at the picture attached: http://i350.photobucket.com/albums/q401/villu89/image001_zpsz5ptj0gw.png). Was this something that has been changed recently so it’s not in RouteData anymore? How can I get current content? We are using CMS.Core 10.3.1 and Commerce 10.2.1.

#176094
Edited, Mar 09, 2017 16:03
Vote:
 

Try something like this (note I haven't tried this code myself when I write this):

// filterContext variable is of type ControllerContext
var currentContent = filterContext.HttpContext.Request.RequestContext.GetRoutedData<IContent>();



#176191
Edited, Mar 13, 2017 16:20
Vote:
 
var contentRouteHelper = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<ContentRouteHelper>();
            var content = contentRouteHelper.Content;
            if (content == null || !(content is CatalogContentBase))
            {
                return;
            }
#176197
Mar 13, 2017 18:01
Vote:
 

Hi! Thank you for the answers! Seems like both methods work just fine!

#177100
Apr 04, 2017 8:40
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.