This tutorial will teach you how you can query Episerver CMS to get the URL for any page. On every Episerver build that you work on, you will need to link pages together in some shape or form. A single page site would be very dull otherwise, however, easy to build 😉. There are several techniques to query Episerver for a page URL, within this tutorial I will cover all the approaches, so you can decide which one works best for you 🔥🔥🔥

The Current Page Url Within Code: To get the Url for the page that a site visitor is currently on (current page), 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 💥

The Current Page Url From Within A Controller: If you are in a block controller, you can get the current page object from the HttpContext object like this:

Again, once you have access to the current page object, you can call it's LinkURL property

The Page Url For A Specific Page: To get the Url of any page, you need to reference it. In Episerver land, this is done by instantiating a ContentReference object and passing it to the IContentRepository API. To create this object, you need the ID of the page. This can be found within the CMS by opening the page and looking in the Url. In this example, I'm going to hard-code the value. If you want to know how to get page IDs in code, I recommend creating a setting page in the CMS.

You can then call this settings page from your code, getting you any key page IDs you need 💥

Get Friendly Url: Sometimes when you render a link in Episerver, you may notice the Url uses an internal link and not the simple name. Using links like this is 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. If you notice an internal link is being rendered, you can get the friendly version instead, like this:

External Url Of A Page: Finally, you can get the full external link to your page, using this code:

You should now have a nice friendly URL that you can use 😊

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 Url in Episerver CMS? Leave a comment below to prove it. Happy Coding 🤘