Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • IanS 22 posts 65 karma points
    Oct 07, 2016 @ 15:42
    IanS
    0

    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.

  • Steve Morgan 1346 posts 4453 karma points c-trib
    Oct 07, 2016 @ 16:31
    Steve Morgan
    0

    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.

        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

    HTH

    Steve

  • IanS 22 posts 65 karma points
    Oct 07, 2016 @ 16:53
    IanS
    0

    That's great Steve, thanks very much.

    I've now got the following code working as planned:

        public static string    GetURL(string alias)
        {
            UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current);
    
            return helper.TypedContentAtRoot().DescendantsOrSelf(alias).FirstOrDefault().Url;
        }
    
  • Nik 1594 posts 7153 karma points MVP 6x c-trib
    Oct 07, 2016 @ 19:31
    Nik
    0

    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

  • IanS 22 posts 65 karma points
    Oct 10, 2016 @ 11:13
    IanS
    0

    Agreed, thanks Nik.

Please Sign in or register to post replies

Write your reply to:

Draft