Search code examples
mvcsitemapproviderasp.net-mvc-sitemap

Adding nodes to mvcsitemap in code


I am building a simple CMS that stores the navigation/sitemap/site structure in a database table. When the app starts up I can create my sitemap nodes dynamically based on the data from that table.

The problem I am having is when a new record is added to the table or an existing one is updated while the app is running I need to rebuild the sitemap structure. Seems simple enough but I cannot figure out how.


Solution

  • There is no built-in support of clearing/rebuilding the sitemap on-the-fly.
    But you could implement this yourself quite easily by implementing a custom sitemapprovider deriving from MvcSiteMapProvider.DefaultSiteMapProvider, and create a method used to clear the cache, using the protected Clear() method. Example:

    public class CustomSiteMapProvider : DefaultSiteMapProvider
    {
      public void ClearSiteMap()
      {
        Clear();
      }
    }
    

    I haven't tried it but it should work. Remember to changed the web.config file to use the custom sitemapprovider instead of DefaultSiteMapProvider.