Get URL from an EPiServer PageReference

Retrieving the URL to an EPiServer page seems like a basic thing to do, but it’s not quite obvious…

Referring to a page is normally represented by an instance of the PageReference class, so obviously that contains a public property called Url or something, right?

Wrong!

Instead, this is how to do it (as a bonus I also show how to retrieve the PageReference from a page property):

string url = "";
PropertyData pd = CurrentPage.Property["Shop_CartPage"];
if (pd != null && pd.IsNull == false)
{
	PageReference pageref = (PageReference)pd.Value;
	url = GetPage(pageref).LinkURL;
}

If anyone knows better or more efficient ways to do this, please add a comment to this post 🙂

Pages without page types in EPiServer

When creating “normal” ASPX pages (i.e. pages that are not EPiServer page types) to use in EPiServer sites it can be very useful to have them inherit from TemplatePage or SimplePage (both are EPiServer types) so that dynamic properties can be read, page methods called, etc. Unfortunately this will give rise to an error message similar to this:

The current template "/shop/Varukorg.aspx" does not match the specified page type file "/default.aspx"

This is because TemplatePage and SimplePage assumes that they are part of an EPiServer page type. This problem can be fixed by overriding ValidatePageTemplate() with an empty implementation:

public override void ValidatePageTemplate()
{
}

Voilá! No more error message.

BTW, try finding this information in the EPiServer docs. Not so easy…

User Controls in EPiServer

This is my first post about EPiServer, which basically is a framework for building web sites. It’s very capable and extensible and is in wide use in Scandinavia. For more information, look here: http://www.episerver.com.

Web User Controls in EPiServer solutions should always inherit from EPiServer.UserControlBase rather than the usual System.Web.UI.UserControl. This gives access to useful functions and properties such as

  • PageData GetPage(PageReference pageLink)
  • PageData CurrentPage
  • string Translate(string key)

Note that those functions work even if the current page is not a “proper” page type in EPiServer so it’s missing from the page hierarchy. Very useful!