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

Can i find out in what page a block is rendered?

Vote:
 

I know a block can belong to a lot of different pages but could it be possible to somehow, during the rendering of the view, use this statement to get the context in which it is in or some other method.

Html.ViewContext.RequestContext.GetContentLink()

When I use the above statement I get SysRoot content link so it obviously does not work as I want it to...

Any suggestions?

#78952
Dec 05, 2013 14:49
Vote:
 
#78959
Dec 05, 2013 15:50
Vote:
 

Hi Joshua!

Thanks for the link but this link is old (no page)... 

#78990
Dec 06, 2013 9:21
Vote:
 

Correct link is http://world.episerver.com/Documentation/Class-library/?documentId=cms/7/6a2d22e8-b20c-9cd8-86d5-fc0259c2029b

You could use it like this:

 

            var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
            var references = contentRepository.GetReferencesToContent(someReference, false);

    

#79008
Dec 06, 2013 10:53
Vote:
 

Ok, I have tried this out:

var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
var references = contentRepository.GetReferencesToContent(((IContent) Model).ContentLink, false);

 

But that gives me references to all content that references this block instance. 
But is it somehow possible for a block to find out (in its cshtml file for instance) in what page it is rendered at that moment?

The thing i would like to achieve is for my block to find out under what SiteRoot (Start Page) it is rendered so it can get some properties from that SiteRoot. One solution is to add a property to my block that holds a PageReference to the SiteRoot but if I would like to use one instance of my block on several different SiteRoots then it will fail..

Any suggestions?

#79018
Edited, Dec 06, 2013 15:04
Vote:
 

Ah, check out this forum topic on how to get the current page:

http://world.episerver.com/Modules/Forum/Pages/Thread.aspx?id=75674&

 

#79019
Dec 06, 2013 15:16
Vote:
 

Thanks! It works exactly as I want it:

var pageRouteHelper = EPiServer.ServiceLocation.ServiceLocator
           .Current.GetInstance<EPiServer.Web.Routing.PageRouteHelper>();
var currentPage = pageRouteHelper.Page;

    

#79058
Dec 09, 2013 9:25
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* 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.