Try our conversational search powered by Generative AI!

Ravindra S. Rathore
Aug 2, 2019
  8402
(10 votes)

Retrieving absolute url of current page on any page or block

Hi,

If you've ever tried to get the full URL of a current page then this blog post may come handy. So basically you will need VirtualPathArguments, That will have ContextMode facility to enable it based on our needs. For instance Default mode, Edit mode, Preview mode and obviously an Undefined mode if the Context has not been defined.

And then obviously the "Uri" method with the "SiteDefinition.Current.SiteUrl".

This is a small blog post that covers the absolute/full URL of the current page on any block or on a page.

            var routeHelper = ServiceLocator.Current.GetInstance<IPageRouteHelper>();
          
            var pageUrl = UrlResolver.Current.GetUrl(routeHelper.Page.ContentLink,
                ContentLanguage.PreferredCulture.Name,
                new VirtualPathArguments { ContextMode = EPiServer.Web.ContextMode.Default });

            var absoluteurl = new Uri(SiteDefinition.Current.SiteUrl, pageUrl);

However, I've created an instance of PageRouteHelper just for a demo purpose but you should always inject your dependencies and get them resolved with IOC container like StructureMap which Episerver recommends.

Thanks

Ravindra

Aug 02, 2019

Comments

Please login to comment.
Latest blogs
GPT-4o Now Available for Optimizely via the AI-Assistant plugin!

I am excited to announce that GPT-4o is now available for Optimizely users through the Epicweb AI-Assistant integration. This means you can leverag...

Luc Gosso (MVP) | May 17, 2024 | Syndicated blog

The downside of being too fast

Today when I was tracking down some changes, I came across this commit comment Who wrote this? Me, almost 5 years ago. I did have a chuckle in my...

Quan Mai | May 17, 2024 | Syndicated blog

Optimizely Forms: Safeguarding Your Data

With the rise of cyber threats and privacy concerns, safeguarding sensitive information has become a top priority for businesses across all...

K Khan | May 16, 2024

The Experimentation Process

This blog is part of the series -   Unlocking the Power of Experimentation: A Marketer's Insight. Welcome back, to another insightful journey into...

Holly Quilter | May 16, 2024