Copied to clipboard

Flag this post as spam?

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


  • Mads Krohn 211 posts 504 karma points c-trib
    Aug 01, 2013 @ 16:49
    Mads Krohn
    0

    Getting NiceUrl outside controller/view context

    Right now, to get a node url, when I'm not in a controller/view context, I'm doing this:

    node.Url = new UmbracoHelper(UmbracoContext.Current).NiceUrl(node.Id);
    

    Is there an easier way to get to the url, or is this the preferred way?

    Running Umbraco v. 6.1.3

    Cheers
    - Mads

  • Stephen 767 posts 2273 karma points c-trib
    Aug 02, 2013 @ 11:01
    Stephen
    100

    Assuming you want the url for a given node ID, what you're doing works. Note that if you have a node object which is an IPublishedContent, you can use the node.Url property. What is "node" in your example? Also what do you mean by "not in a controller/view"?

    The simplest way I can think of would be:

    var url = UmbracoContext.Current.UrlProvider.GetUrl(id);

    Unfortunately at the moment the UrlProvider property is internal so that won't work in a controller or a view. I'm wondering why it's internal, though. Have created issue http://issues.umbraco.org/issue/U4-2581 to discuss it.

    In the meantime, what you do is correct, just create your own UmbracoHelper.

    Cheers,
    Stephan

  • Stephen 767 posts 2273 karma points c-trib
    Aug 02, 2013 @ 11:04
    Stephen
    1

    Just to broaden the discussion: can we list all the different helpers that we know of (uQuery, etc) and devise a "standard scheme" so that we can tell people "that's how you get a node, get the url, get..." once and for all?

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Aug 02, 2013 @ 11:33
    Lee Kelleher
    0

    I'm still using umbraco.library.NiceUrl(1234) to get a node's URL.

    uQuery doesn't have an explicit "get NiceUrl", as it's mostly a wrapper for the umbraco.NodeFactory classes.

    e.g. if you wanted a node, we would go: uQuery.GetNode(1234).NiceUrl

    Cheers, Lee.

  • Mads Krohn 211 posts 504 karma points c-trib
    Aug 02, 2013 @ 13:31
    Mads Krohn
    1

    Thank you for replying Stephen.
    What node is, is in theory irrelevant, but in this particular instance, it's a wrapper for an Examine result, where I need the url of the result node, hence, I just wan't the url and nothing else.

    My reason for asking was, that creating helpers like this feels .. wrong .. I agree that UmbracoContext.Current.UrlProvider.GetUrl(id); should be public, while it's a long one to type, at least it feels more right :) Was just wondering if I missed a more "correct" way of doing it, hence the discussion and need for a "standard scheme". I'll keep creating an UmbracoHelper for now :)

    Lee, I really wan't to stay clear of the old api for what I'm doing here, and besides, library.NiceUrl just creates a new UmbracoHelper under the covers anyways :P Thanks for tipping in anyways though, it's always appreciated!

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Aug 02, 2013 @ 13:39
    Lee Kelleher
    0

    While UmbracoContext.Current.UrlProvider.GetUrl(1234) makes logical sense (to me), it feels too verbose.

    Would a static helper be better?

    e.g. nuQuery.GetUrl(1234) ... *of course with nuQuery being whatever would be agreed for a static class? ;-)

    Maybe this discussion is better for the mailing list? (Happy to follow wherever).

    Cheers, Lee.

  • Mads Krohn 211 posts 504 karma points c-trib
    Aug 02, 2013 @ 13:43
    Mads Krohn
    0

    Well, for normal use, one will never have to care, as you would just get the node.Url from the IPublishedContent, so while verbose, I don't mind the UmbracoContext.Current.UrlProvider.GetUrl(1234) syntax. If I'm in a position where I need to use that syntax, I'll probably be able to hide it away in a wrapper somewhere myself.

    But I agree, this is probably better suited for the mailing list :)

    Cheers - Mads

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Aug 02, 2013 @ 17:13
    Hendy Racher
    0

    Hi,

    I think there's something to be said for a static helper that enables a consistant API  - regardless as to where the call is being made. Hopefully the conversation will continue (https://groups.google.com/forum/#!forum/umbraco-dev

    Cheers,

    Hendy

Please Sign in or register to post replies

Write your reply to:

Draft