In this tutorial, you will learn about the API you need to use in order to get the friendly URL for a page within a Umbraco CMS powered project when using pages of type IContent. Umbraco comes with two ways to access page data. Data that comes from the Umbraco cache are of type IPublishedContent. The APIs to access pages returned as IPublishedContent are quick to access because they are read-only and come from cache. If you need to perform read/write operations on-page data you will be working with objects of type IContent. Any Umbraco API that returns data as type IContent are slower because the API will query the database rather than a cache. The benefit of using these database based APIs is that you can perform read/write operations on these objects. If you need to get data this way, you may want to consider if your architecture is correct. If you can work with IPublishedContent instead, your code will be slightly more performant.

The IContent interface does not contain a Url property. This is mainly because Umbraco might not be published types of IContent yet and the Url might not exist yet. If you know a page of type IContent is published and you need to access the Url for a page regardless then read on. One word of caution, you may want to consider if your architecture is correct if you need to do this. If you can work with IPublishedContent instead, your life will be easier and your code quicker!. To access the Url when working with IContent, you will need to use the Umbraco UrlProvider. The code to do this is shown below:

Assuming you have access to an item of type IContent. On Line3, UrlProvider can be used to access the URL. The request Mode will need to be set to use Umbraco.Web.Routing.UrlProviderMode.AutoLegacy as seen on Line4. After doing this, you can use GetUrl() to access the Url, simples!

The other way would be to convert the item into IPublishedContent. This can be done using this code:

Finally, if you want a super safe version, I suggest you copy this code, written by Jeroen Breuer. Happy Coding 🤘