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.
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?
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!
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 :)
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)
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:
Is there an easier way to get to the url, or is this the preferred way?
Running Umbraco v. 6.1.3
Cheers
- Mads
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:
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
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?
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.
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 anUmbracoHelper
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 newUmbracoHelper
under the covers anyways :P Thanks for tipping in anyways though, it's always appreciated!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 withnuQuery
being whatever would be agreed for a static class? ;-)Maybe this discussion is better for the mailing list? (Happy to follow wherever).
Cheers, Lee.
Well, for normal use, one will never have to care, as you would just get the
node.Url
from theIPublishedContent
, so while verbose, I don't mind theUmbracoContext.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
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
is working on a reply...