How to get the Current Page URL in Sitecore

There will be scenarios where you need to restrict/display certain data based on particular URL. In this article let’s take a look at how to get the current page URL in Sitecore.

We know that to read the current item in Sitecore, we can always use Sitecore.Context. Sitecore Context is similar to HttpContext in traditional Below code will return the current context Item in Sitecore.

var currentItem= Sitecore.Context.Item;

Get the Current Page URL in Sitecore

There are multiple ways to get the current page URL in Sitecore rather I would say the traditional way of getting the current page URL and the MVC way of finding the current page URL in Sitecore.

Approach 1 – Fetch Current Page URL in Sitecore

We can use the LinkManager.GetItemURL and pass the current item as a parameter to fetch the current page URL. Below is the sample code of it.

var currentURL= Sitecore.Links.LinkManager.GetItemUrl(Sitecore.Context.Item);

Fetch Particular URL in Sitecore
In order to fetch a particular item URL in the Sitecore,  we need to fetch the item and then you need to pass the item to the LinkManager.GetItemUrl() method.

var productItem= Sitecore.Context.Database.GetItem("/sitecore/content/Home/Product1");
var product1URL= Sitecore.Links.LinkManager.GetItemUrl(productItem);

Approach 2- Fetch Current Page URL in Sitecore using MVC

This is not the recommended approach as it would break most of the MVC design pattern and architecture rules. The business logic and any Sitecore logics should be separated and it should not appear in the View.

However, if you still like to fetch the current page URL then the good news is there are many MVC helpers available which will help you out in getting the current URL.

PageItem is the item that is being rendered on the page. It is the one which carries the rendering to the Layout definition.

var currentURL = PageContext.Current.Item;


Leave a Reply

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

You May Also Like
XOR in Python

XOR in Python

XOR Operator in Python is also known as “exclusive or”  that compares two binary numbers bitwise if two bits are…
How To

How to configure Git proxy?

If you are working in the corporate company all the internet requests usually goes through the corporate firewall.…