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 🙂
Hey there,
Yeah this is the only way to get the page URL, but to tidy it up a bit I would use the IsValue method of the base classes to check if the page has the property and the property has a value:
Thanks for your input. Your variant is indeed a bit tidier, although I personally prefer doing it without having to type the property name twice. If it changes or is spelled wrong there are two lines to modify and it’s easy to miss one of them. Been there, done that 😉
On the other hand, your condition is simpler which is also important. Hmm…
See you around, Jeremy. BTW, might you be the Jeremy of http://episervernz.blogspot.com? I’ve visited your site a few times, good work!
Emil
Thanks guys. Are we still at this stage today, of is there a better way in EPiServer CMS 6?
Sorry for late answer…
Unfortunately I don’t know the answer to that since I haven’t worked with later EPiServer versions. I’ve been too busy working in non-EPi projects…
/Emil
Thanks! It solved my problem.