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 369 posts 701 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 6133 posts 23952 karma points MVP 7x 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 374 posts 1346 karma points
    Jan 16, 2018 @ 22:23
    Marcio Goularte
    0
    var content= UmbracoContext.Current.ContentCache.GetById(nodeId);
     content.Url;
    
  • Søren Kottal 702 posts 4497 karma points MVP 5x 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 12133 karma points MVP 8x 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 369 posts 701 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 4124 posts 22215 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)

Please Sign in or register to post replies

Write your reply to:

Draft