Join us this Friday for AI in Action at the Virtual Happy Hour! This free virtual event is open to all—enroll now on Academy and don’t miss out.

 

Ravindra S. Rathore
Aug 2, 2019
  9669
(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
Best Bets, synonyms and wildcard queries

When using a wildcard in your search query like ‘search.For(query+”*”)’ or when you used the reversed method suggested in the Breaking changes...

Jeroen Stemerdink | Jan 28, 2025 | Syndicated blog

Content statistics Blazor component

Another week and another MudBlazor component to explore. I wanted to test the charts components so I created a small Blazor component that displays...

Per Nergård (MVP) | Jan 28, 2025

Referencing Page Specific Blocks with ISelectionFactory

A content modeling exercise got me thinking about reuse of page-specific content. It turns out that Optimizely has some good tools to handle this...

Nicholas Sideras | Jan 28, 2025 | Syndicated blog

Further Enhancing Production Database Scalability in DXP Cloud Services

About a year ago we announced that we are improving the scalability of SQL databases in DXP Cloud Services , focusing first on non-production...

Anna Pleshakova | Jan 27, 2025

Automatic Alt-Text for Images with AI Assistant for Optimizely

Creating accessible and user-friendly content has never been more critical. For websites, ensuring that images include descriptive alt-text is an...

Luc Gosso (MVP) | Jan 26, 2025 | Syndicated blog

Optimizely CMS easy RSS feed integration library

As I've mentioned in my  previous blog post , while I was developing the Optimizely version of my blog, I tried to look for a library that could...

David Drouin-Prince | Jan 25, 2025 | Syndicated blog