Copied to clipboard

Flag this post as spam?

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


  • Rob Watkins 370 posts 702 karma points
    Jan 16, 2018 @ 16:31
    Rob Watkins
    0

    Best practice for getting content URLs in v7

    Quick question; in application code, what is the current best practice for getting a URL from a node ID, e.g. as returned from Examine?

    Is it still umbraco.library.NiceUrl()?

  • Alessandro Calzavara 32 posts 144 karma points c-trib
    Jan 16, 2018 @ 16:57
    Alessandro Calzavara
    0
    var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
    var node = umbracoHelper.TypedContent(nodeId);
    if(node != null)
    {
        return node.Url;
    }
    else
    {
        return null;
    }
    

    not sure if there's a faster way

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Jan 16, 2018 @ 21:18
    Alex Skrypnyk
    0

    Hi Rob

    You are right, this is the way:

    umbraco.library.NiceUrl()
    

    Thanks,

    Alex

  • Marcio Goularte 389 posts 1362 karma points
    Jan 16, 2018 @ 22:23
    Marcio Goularte
    0
    var content= UmbracoContext.Current.ContentCache.GetById(nodeId);
     content.Url;
    
  • Søren Kottal 713 posts 4571 karma points MVP 6x c-trib
    Jan 18, 2018 @ 08:28
    Søren Kottal
    0

    Theres a NiceUrl() and NiceUrlWithDomain() method on the UmbracoHelper. I would use that.

    You can learn more about the UmbracoHelper here:

    https://our.umbraco.org/Documentation/Reference/Querying/UmbracoHelper/

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Jan 18, 2018 @ 09:10
    Dave Woestenborghs
    105

    Hi Rob,

    I would not use umbraco.library.NiceUrl because it's a legacy API. If i'm correct it will deprecated in V8

    In c# code you can use :

     UmbracoContext.Current.UrlProvider.GetUrl(id);
    

    In your views you can use :

    @Umbraco.Url(id)
    

    Dave

  • Rob Watkins 370 posts 702 karma points
    May 01, 2018 @ 08:08
    Rob Watkins
    0

    Perfect answer - C# plus Razor examples, exactly what I was after. Thanks Dave.

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Nov 25, 2019 @ 09:43
    Matt Brailsford
    0

    Just as a correction to this, in your views it's not @(Umbraco.Url(id)) it's actually @(UmbracoContext.Url(id)) (Quite possible this changed since original posting)

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies