I'm wanting to make a static method that I can call from anywhere - i.e. from a MVC razor page, a controller or any other static method in any class - that will give me the URL of any page and I'm having trouble find a way of doing this.
I'm wanting a method I can pass in an 'alias' of a page to and it'll give me the URL back.
Every technique I've found seems to need a 'Model' of some kind as a starting point which isn't very useful. And each technique seems to require you traverse nodes.
It seems like such a fundamental thing so am surprised I can't find anything. How do people typically set up nav links, footer links etc?
I tend to put "global" settings and content like navs and footers in a site settings node.
From Razor (in views) you can "get" this node - either by going to the root node and looking for the first "SiteSettings" node or, my preference, to get it by node Id. During dev you'll create the site settings node before peeling off Uat and live versions so that ID is never going to change - there is a risk content editors will delete it I suppose but by the same argument they can delete the homepage too!
Then you can "get" the Site settings node on your master template or where ever you need it.
var siteSettingsNode = Umbraco.TypedContent(1234));
// or you could get the siteSettingsNode by having a value set on a picker on the root home node?
// var siteSettingsNode = Umbraco.TypedContent(rootNode.GetPropertyValue("siteSettingsNode"));
Then you just have to use the siteSettings Node like you would Model.Content
// Example below is using the RJP Multi Url Picker for footer links.
@if (siteSettingsNode.HasValue("FooterCol1Links"))
{
var col1Links = siteSettingsNode.GetPropertyValue<MultiUrls>("FooterCol1Links");
if (col1Links.Any())
{
<ul>
@foreach (var footerLink in col1Links)
{
<li><a href="@footerLink.Url" target="@footerLink.Target">@footerLink.Name</a></li>
}
</ul>
}
}
In certain circumstances (MVC etc) you'll have to use an Umbraco Helper
Just an observation. I think that if you are calling FirstOrDefault you have a potential null exception that could be thrown.
If I remember rightly, it is possible for FirstOrDefault to return null if there are no DescendantsOrSelf for the requested alias. As a result when you call ".Url" your code will thrown an exception.
It would be worth putting some errorhandling/logic in place to cater for this scenario.
Global method for getting URL of any page
Hello
I'm wanting to make a static method that I can call from anywhere - i.e. from a MVC razor page, a controller or any other static method in any class - that will give me the URL of any page and I'm having trouble find a way of doing this.
I'm wanting a method I can pass in an 'alias' of a page to and it'll give me the URL back.
Every technique I've found seems to need a 'Model' of some kind as a starting point which isn't very useful. And each technique seems to require you traverse nodes.
It seems like such a fundamental thing so am surprised I can't find anything. How do people typically set up nav links, footer links etc?
Any help would be great.
Hi Ian - welcome to Our Umbraco!
I tend to put "global" settings and content like navs and footers in a site settings node.
From Razor (in views) you can "get" this node - either by going to the root node and looking for the first "SiteSettings" node or, my preference, to get it by node Id. During dev you'll create the site settings node before peeling off Uat and live versions so that ID is never going to change - there is a risk content editors will delete it I suppose but by the same argument they can delete the homepage too!
Then you can "get" the Site settings node on your master template or where ever you need it.
Then you just have to use the siteSettings Node like you would Model.Content
In certain circumstances (MVC etc) you'll have to use an Umbraco Helper
HTH
Steve
That's great Steve, thanks very much.
I've now got the following code working as planned:
Hi Ian,
Just an observation. I think that if you are calling FirstOrDefault you have a potential null exception that could be thrown.
If I remember rightly, it is possible for FirstOrDefault to return null if there are no DescendantsOrSelf for the requested alias. As a result when you call ".Url" your code will thrown an exception.
It would be worth putting some errorhandling/logic in place to cater for this scenario.
Nik
Agreed, thanks Nik.
is working on a reply...