How to get the URL from an IContent instance

How do I get (or build) the Url from an IContent instance? In Umbraco 7 the following code worked:

string url = UmbracoContext.Current.UrlProvider.GetUrl(content.Id);

but in Umbraco 10 the IUrlProvider interface requires an IPublishedContent instance. Interestingly enough, the DefaultUrlProvider’s implementation only uses the IPublishedContent.Id property so an overload which accepted just the content Id would work fine (but alas doesn’t exist anymore. why?).

Note, the assumption here is that the IContent instance is not published (although it might be).

One work-around would be to create an “empty” IPublishedContent instance with just the Id property assigned (how do I do that, is there a suitable/lightweight class implementing IPublishedContent somewhere in the framework?).

Another work-around would be to “fake” the Url by building one using IUrlSegmentProvider.GetUrlSegment() for each of the IContent’s parents. Is there a helper method somewhere already doing this?


This is a companion discussion topic for the original entry at https://our.umbraco.com/forum/110007-how-to-get-the-url-from-an-icontent-instance