Skip to content

Instantly share code, notes, and snippets.

@petergledhill
Created April 8, 2015 08:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petergledhill/ca2a3a0ea81b06abcb08 to your computer and use it in GitHub Desktop.
Save petergledhill/ca2a3a0ea81b06abcb08 to your computer and use it in GitHub Desktop.
Umbraco : get relative URL of IContent without UmbracoContext
public static class ContentExtensions
{
public static string RelativeUrl(this IContent content)
{
var pathParts = new List<string>();
var n = content;
while (n != null)
{
pathParts.Add(n.UrlName());
n = n.Parent();
}
pathParts.RemoveAt(pathParts.Count() - 1); //remove root node
pathParts.Reverse();
var path = "/" + string.Join("/", pathParts);
return path;
}
public static string UrlName(this IContent content)
{
return new DefaultUrlSegmentProvider().GetUrlSegment(content).ToLower();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment