Creating the AutoSiteMapProvider : Site Maps « Development « ASP.NET Tutorial






File: App_Code/AutoSiteMapProvider.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Caching;

namespace MyNamespace
{
    public class AutoSiteMapProvider : StaticSiteMapProvider
    {
        private SiteMapNode _rootNode;
        private static List<string> _excluded = new List<string>();
        private List<string> _dependencies = new List<string>();

        static AutoSiteMapProvider()
        {
            _excluded.Add("app_code");
            _excluded.Add("app_data");
            _excluded.Add("app_themes");
            _excluded.Add("bin");
        }

        protected override SiteMapNode GetRootNodeCore()
        {
            return BuildSiteMap();        
        }

        public override SiteMapNode BuildSiteMap()
        {
            lock (this)
            {
                HttpContext context = HttpContext.Current;
                _rootNode = (SiteMapNode)context.Cache["RootNode"];
                if (_rootNode == null)
                {
                    Clear();

                    string folderUrl = HttpRuntime.AppDomainAppVirtualPath;
                    string defaultUrl = folderUrl + "/Default.aspx";
                    _rootNode = new SiteMapNode(this, folderUrl, defaultUrl, "Home");
                    AddNode(_rootNode);

                    AddChildNodes(_rootNode);
                    _dependencies.Add(HttpRuntime.AppDomainAppPath);

                    CacheDependency fileDependency = new CacheDependency(_dependencies.ToArray());
                    context.Cache.Insert("RootNode", _rootNode, fileDependency);
                }
                return _rootNode;
            }
        }

        private void AddChildNodes(SiteMapNode parentNode)
        {
            AddChildFolders(parentNode);
            AddChildPages(parentNode);
        }        
        private void AddChildFolders(SiteMapNode parentNode)
        {
            HttpContext context = HttpContext.Current;
            string parentFolderPath = context.Server.MapPath(parentNode.Key);
            DirectoryInfo folderInfo = new DirectoryInfo(parentFolderPath);

            DirectoryInfo[] folders = folderInfo.GetDirectories();
            foreach (DirectoryInfo folder in folders)
            {
                if (!_excluded.Contains(folder.Name.ToLower()))
                {
                    string folderUrl = parentNode.Key + "/" + folder.Name;
                    SiteMapNode folderNode = new SiteMapNode(this, folderUrl, null, GetName(folder.Name));
                    AddNode(folderNode, parentNode);
                    AddChildNodes(folderNode);
                    _dependencies.Add(folder.FullName);
                }
            }
        }

        private void AddChildPages(SiteMapNode parentNode)
        {
            HttpContext context = HttpContext.Current;
            string parentFolderPath = context.Server.MapPath(parentNode.Key);
            DirectoryInfo folderInfo = new DirectoryInfo(parentFolderPath);

            FileInfo[] pages = folderInfo.GetFiles("*.aspx");
            foreach (FileInfo page in pages)
            {

                if (!_excluded.Contains(page.Name.ToLower()))
                {

                    string pageUrl = parentNode.Key + "/" + page.Name;
                    if (String.Compare(pageUrl, _rootNode.Url, true) !=0)
                    {                        SiteMapNode pageNode = new SiteMapNode(this, pageUrl, pageUrl, GetName(page.Name));
                        AddNode(pageNode, parentNode);
                    }
                }
            }
        }

        private string GetName(string name)
        {
            name = Path.GetFileNameWithoutExtension(name);
            return name.Replace("_", " ");
        }
    }
}
  


File: Web.Config

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
    <system.web>

      <siteMap defaultProvider="MyAutoSiteMapProvider">
        <providers>
          <add
            name="MyAutoSiteMapProvider"
            type="MyNamespace.AutoSiteMapProvider" />
        </providers>
      </siteMap>

    </system.web>
</configuration>








9.40.Site Maps
9.40.1.Defining a Site Map
9.40.2.The SiteMapPath control enables you to navigate to any parent page of the current page.
9.40.3.Format SiteMapPath
9.40.4.Using a template with the SiteMapPath control.
9.40.5.Using the SiteMap Class
9.40.6.Adding nodes to a Site Map dynamically.
9.40.7.Using the SiteMapNode Class
9.40.8.To display different links to different users depending on their roles, enable the Security Trimming
9.40.9.Merging Multiple Site Maps
9.40.10.Creating Custom Site Map Attributes
9.40.11.Creating the AutoSiteMapProvider
9.40.12.Displaying an automatically generated Site Map.
9.40.13.Removing the root node from the retrieved node collection
9.40.14.using the StartFromCurrentNode property
9.40.15.Using the StartingNodeUrl property
9.40.16.Working with the CurrentNode object (C#)
9.40.17.Working with the CurrentNode object (VB)
9.40.18.Creating a custom navigation display using the CurrentNode property (C#)
9.40.19.Creating a custom navigation display using the CurrentNode property (VB)