Get Full Relative URL for the child pages [Better URL Rewriting]

Topics: ASP.NET 2.0, Business Logic Layer
Nov 19, 2010 at 5:33 PM
Edited Nov 20, 2010 at 3:34 PM

I notice a good feature in pages managment, but it not used a lot, It's Parent page feature.

this feature "As I think" used only in the admin pages "the old page" to show you the tree of pages ,So I do a search in the code on the Property "HasParent" and I didn't found any use for it even in the new admin pages page, So I did some tweaks to use it to show full relative URL of the page in the address bar and here is my tweak in Page.cs file in the core project:

Replace this property

 /// <summary>
        ///     Gets a relative-to-the-site-root path to the page.
        ///     Only for in-site use.
        /// </summary>
        public string RelativeLink
        {
            get
            {
                var theslug = Utils.RemoveIllegalCharacters(this.Slug) + BlogSettings.Instance.FileExtension;
                return string.Format("{0}page/{1}", Utils.RelativeWebRoot, theslug);
            }
        }
        With this code
  /// <summary>
        ///     Gets a relative-to-the-site-root path to the page.
        ///     Only for in-site use.
        /// </summary>
        public string RelativeLink
        {
            get
            {
                var theslug = Utils.RemoveIllegalCharacters(this.Slug);
                if (this.HasParentPage)
                    theslug = string.Format("{0}{1}{2}", getParentSlug(this), theslug, BlogSettings.Instance.FileExtension);
                else
                    theslug += BlogSettings.Instance.FileExtension;
                return string.Format("{0}page/{1}", Utils.RelativeWebRoot, theslug);
            }
        }

        /// <summary>
        ///  Recursive method gets Parent Slug regardless how deep is it
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        string getParentSlug(Page page)
        {
            var parent = GetPage(page.Parent);
            string parentSlug = Utils.RemoveIllegalCharacters(parent.Slug);
            if (parent.HasParentPage)
                parentSlug = getParentSlug(parent) + parentSlug;
            return parentSlug + "/";
        }
So for example 
if you have MyProjects Page parent to Project1 Page So :
the URL for MyProjects will be www.domain.com/Page/MyProjects.aspx and 
the URL for Project1 will be www.domain.com/Page/MyProjects/Project1.aspx