In this tutorial, you will learn how to get the Url for any Sitecore page object in code. Sitecore provides several APIs that will allow you to get the Url of an item. If you want to learn about all these different APIs and when you should use each approach, read on 🔥🔥🔥

Sitecore Link Manager

The LinkManager was introduced in Sitecore 6 and provides a fairly comprehensive set of features to access Urls within Sitecore. All you need to do is pass in the Sitecore Item object that you are interested in and the LinkManager will then return the Url. To get a Url from LinkManager, you can use this code:

To access a Url you can pass the Sitecore Item into the GetItemUrl() method within LinkManager. GetItemUrl() also has a few overloads to allow you to define extra configuration. The code below shows how to enable the AlwaysIncludeServerUrl flag:

Setting AlwaysIncludeServerUrl will tell LinkManager to return the full Url. An example of a full Url is shown below:

If you are working on a multi-language project, you may want to get the Urk based on a particular language. This is done using the LanguageEmbedding overload:

When the LanguageEmbedding property is set, LinkManager will return the following Url:

How To Get The Raw Sitecore Url

In some situations, you might just want to get information about the current request. You can do this directly from the Sitecore context, using the RawUrl property:

This returns a Url that will look similar to the LinkManager with LanguageEmbedding enabled call:

How To Get The Sitecore Url For Media

If you need to get the Url for a media item, you will need to use the MediaManager. The MediaManager works in a very similar way as LinkManager. You call the GetMediaUrl() method and pass in some configuration options:

How To Get The Sitecore Url Within A Razor View

If you are using MVC and want the current Url in a view, you can use the Model.Url() property. Model.Url() will return a relative Url for the current object, e.g. in this format /terms-and-conditions

You now know the main ways of getting Urls from Sitecore. The most used technique to access a Url in backend code is the LinkManager. As you have seen, you have a lot of other options available to you. Happy Coding 🤘