Ravindra S. Rathore
Aug 2, 2019
  8830
(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
Creating an Optimizely CMS Addon - Adding an Editor Interface Gadget

In   Part One   of this series, I covered getting started with creating your own AddOn for Optimizely CMS 12. This covered what I consider to be an...

Mark Stott | Aug 30, 2024

Configure your own Search & Navigation timeouts

The main blog Configure your own Search & Navigation timeouts was posted for years but you need copy the code to your application. We now bring tho...

Manh Nguyen | Aug 30, 2024

Joining the Optimizely MVP Program

Andy Blyth has been honoured as an Optimizely MVP, recognising his contributions to the Optimizely community. Learn how this achievement will enhan...

Andy Blyth | Aug 29, 2024 | Syndicated blog

Welcome 2024 Summer OMVPs

Hello, Optimizely community! We are thrilled to announce and welcome the newest members to the Optimizely Most Valuable Professionals (OMVP) progra...

Patrick Lam | Aug 29, 2024