AI OnAI Off
Normally you inherit from TemplatePage or UserControlBase which gives you access to CurrentPage.
I have never encountered any situation in webforms where you would like to render a menu that is aware of the current page outside a page or user control.
So, it's probably possible to do this, but it's not common practice, so maybe you should rethink the solution?
Hi Mark,
I would simplify the navigation logic a bit.
public class NavigationProvider
{
public List<NavigationData> GetNavigationData(ContentReference currentPage, INavigationFilter filter)
{
....
}
}
public static class Html
{
public static IHtmlString PrimaryNavigation(ContentReference currentPage)
{
var navigationProvider = ServiceLocator.Current.GetInstance<NavigationProvider>();
var filter = new PrimaryNavigationFilter();
var data = navigationProvider.GetNavigationData(currentPage, filter);
...
}
}
And as Fredrik mentioned, if you inherit the view from TemplatePage or UserControlBase, you'll have access to CurrentPage
Actually just stumbled across a way of doing it in the Alloy demo, they use it for the master page which doesn't seem to get any specific EPiServer base class or context.
As such something like this actually works:
public static class Site { public static PageData CurrentPage { get { if (HttpContext.Current == null) throw new NotSupportedException("Cannot get current page without HttpContext"); var pageBase = HttpContext.Current.Handler as PageBase; if (pageBase == null) throw new NotSupportedException("Cannot get current PageBase object"); var currentPage = pageBase.CurrentPage; if (currentPage == null) throw new NotSupportedException("Current page is not set."); return currentPage; } } }
That said I don't know if it is the best way.
One option is to get an instanse of EPiServer.Web.Routing.ContentRouteHelper (or the similar class PageRouteHelper if you know the routed item in the context is always a PageData instance) from the IOC container.
That will give you the content/page that the current request was routed to.
I've been trying to get the current page outside of the context of an web page or user control but still as part of a http request. I'm quite new to EPiServer and am building a webforms site at the moment and have been tackling the navigation, for which none of the inbuilt controls seem to be particularly useful to me.
Anyway I've created some classes to help wrap what I want but wondered how best to get the current page. The first 2 things I looked at were:
PageReference.SelfReference
DataFactory.Instance.CurrentPage
Neither of these seem to return the current PageReference or PageData.
What I have currently is:
With the idea I can then create my own filters like so:
And render the menu using the following:
However I'm stumbling over checking whether the PageReference or PageData is the current page, or at least the PageReference for the current page passed into my Navigation object.
I appreciate the rendering is a bit MVCish in a WebForms world but the course I was on only covered WebForms but WebControls are more of a PITA than they're worth. Anyway the only real problem with it is my current uncertainty over how best to get the current page.
I had hoped something like the following might have worked: