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?
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.
Views
Site A
Blocks
Site B
Blocks
Common
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?
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?
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.
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?
Thanks,
Vincent