Try our conversational search powered by Generative AI!

Is it possible to get block data via ContentRouteHelper?


Hi EPi team

I need to get the block data during the class initialization and assign to a local variable. Is it possible to get this value from ContentRouteHelper? if not, what could be the best way to get it?



Sep 09, 2016 3:45

Hmm what kind of block are you trying to get? Shared / local block. Are you trying to get it from current page before you get access to it in the controller or similar? Any reason IContentRepository won't do the job?

Sep 09, 2016 8:50

Hi Daniel

Hmm what kind of block are you trying to get? Shared / local block.

Shared Block

Are you trying to get it from the current page before you get access to it in the controller or similar?

No, my block has its controller, I am trying to inject something in my constructor, to get my block data and set to a local variable in order to eliminate some redundant controllers. 

The current site uses one code base share being shared among 5 different sites, each site has its own view folder.


   Site A


    Site B



The current implementation creates controller for each block and resolves the view path inside the Index method. 

public override ActionResult Index(BlockOne currentBlock)
            var viewPath = currentBlock.UseSiteView ? SiteConfiguration.Current().SiteSettings.SettingsPage.TemplateSettings.WebsiteSpecificTemplatePath : "~/Views/Common";

            return PartialView(viewPath + "/Blocks/BlockOne/Index.cshtml", currentBlock);

Any thoughts? 

Sep 09, 2016 9:39

If you just need to get the current block in constructor or similar you can do

public ContactBlockController(IContentLoader contentLoader, IPermanentLinkMapper permanentLinkMapper)
            _contentLoader = contentLoader;
            _permanentLinkMapper = permanentLinkMapper;
            var contentRouteHelper = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<EPiServer.Web.Routing.ContentRouteHelper>();
            var currentBlock = contentRouteHelper.Content;

If you have several similar blocks and controllers it might be worth considering a base class for them and then create a controller the base class instead?

Sep 09, 2016 10:11

I just pass the contentroutehelper into constructor, it can be resolved. I think epi has already registered it in the container.

However, the result from contentroutehelper.content gives me the page data, not block data.

Sep 09, 2016 10:56

Hey Daniel, 

I've attached my test snippet, as you can see the content retrieved from block controller is the page data, not block data self. 

Any thoughts?

Content is pagedata, not blockdata



Sep 12, 2016 3:08
* 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.