How to get the page URL of a specific Episerver page?

Generating links and anchor tags to other areas within your website is a very common requirement in a project, and luckily, the EPi API makes it very easy to do.  There are several ways to ge the current page Url, so here goes:

To get the current pages Url

To get the Url for the page a user is currently on, the first thing you need to do is get a reference to the current page, this can be done using the PageRouteHelper.  Once you have the current page, you can call it’s LinkURL property and job done.

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

To get the current pages from a block

If you are in a block then you can will need to get the page data directly out of the HttpContext

public static string CurrentUrl
{
get
{
var pageBase = HttpContext.Current.Handler as PageBase;
var currentPage = pageBase.CurrentPage;
if (currentPage == null)
return string.Empty
return currentPage.LinkURL;
}
}

To get the URL of another page

To get a referee to another page, you will need a reference to it, e.g.a ContentReference or the page ID of it.  In this example, I’m going to hard-code the value in but in your production code.  I would recommend either adding a property on the page type you are working on, or, to create a specific Settings Page Type that would store all my important URL’s for me.  On most projects, these links may include the Search Page, Contact Us Page, About Page etc…

var contentReference= new ContentReference(21);
var repository = ServiceLocator.Current.GetInstance<IContentRepository>();
var page repository.Get<MyContentType>(contentReference);
var url = page .LinkURL;

How To Get the Friendly URL page

Using the methods above when the link is rendered on the website you will probably notice very quickly that the links are not very SEO friendly.  Having friendly Url’s will improve your websites SEO ratings and will generally make your website easier to use for people.  In order to to render your links in a ‘friendly’ manner you need to do the following:

var pageRouteHelper = ServiceLocator.Current.GetInstance<EPiServer.Web.Routing.PageRouteHelper>();
var currentPageUrl = GetFriendlyURL(pageRouteHelper.Page.ContentLink);
public static string GetVirtualPath(ContentReference pageLink)
{
return ServiceLocator.Current.GetInstance<UrlResolver>()
.GetVirtualPath(pageLink)
.VirtualPath;;
}

Get The External Url Of A Page

private static string GetExternalUrl(ContentReference contentReference)
{
var internalUrl = UrlResolver.Current.GetUrl(contentReference.ContentLink);
var url = new UrlBuilder(internalUrl);
Global.UrlRewriteProvider.ConvertToExternal(url, null, System.Text.Encoding.UTF8);
return UriSupport.AbsoluteUrlBySettings(url.ToString());
}

For a more in-depth overview of how friendly Url’s work have a read of my other article Creating a friendly Url within your Episerver 7 MVC View

Has this article solved your problem or do you think you know a better way to get a link?  Leave a comment below to prove it.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *