EpiServer how to get the Link URL of a specific page?

4.7k views Asked by At

I need to bind the LinkURL of the Blog Page with a link button on the Start Page. What I did was actually found that Page ID and get a Page Reference using it.

PageReference BlogPageReference = new PageReference(21);

PageData BlogPage = GetPage(BlogPageReference);

var url = BlogPage.LinkURL;

This is pretty straight forward, but I'm not happy that the Page ID is hard coded. Is there a better way of doing this, like getting the Page by Page name? or any other way?

Thanks in advance :)

1

There are 1 answers

0
Andreas On BEST ANSWER

I would create a property on the start page of type "Page", which means the property will have the type PageReference. Then it's no longer hardcoded.

It's also common to move such "settings" properties to a separate Settings page type which is itself linked via a property from the root or startpage (which are constants).

Im writing from memory so excuse any mistakes in the code.

var startPage = DataFactory.Instance.Get<StartPage>(PageReference.StartPage);
var settingsPage = DataFactory.Instance.Get<SettingsPage>(startPage.SettingsPage);
var blogPageRef = settingsPage.BlogPage;

Where SettingsPage and BlogPage are defined

public virtual PageReference xxxPage {get; set; }

in your page type class.