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 343 posts 593 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 5533 posts 20809 karma points MVP 3x 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 353 posts 1237 karma points
    Jan 16, 2018 @ 22:23
    Marcio Goularte
    0
    var content= UmbracoContext.Current.ContentCache.GetById(nodeId);
     content.Url;
    
  • Søren Kottal 466 posts 2983 karma points MVP 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 3227 posts 10915 karma points MVP 4x admin c-trib
    Jan 18, 2018 @ 09:10
    Dave Woestenborghs
    104

    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 343 posts 593 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 2409 posts 11695 karma points MVP 6x 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)

Please Sign in or register to post replies

Write your reply to:

Draft