Created
April 8, 2015 08:51
-
-
Save petergledhill/ca2a3a0ea81b06abcb08 to your computer and use it in GitHub Desktop.
Umbraco : get relative URL of IContent without UmbracoContext
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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