This is a migrated thread and some comments may be shown as answers.

Menu: You must have SiteMap defined with key "Web" in ViewData dictionary.

0 Answers 106 Views
Menu
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Phil
Top achievements
Rank 2
Phil asked on 19 May 2012, 10:20 PM
Hi:

No question, just my solution.  The first thing to do in MVC or Asp.Net is to create your own base class.  I added an empty controller called BaseController.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Telerik.Web.Mvc;
 
namespace Mvc3App.Controllers
{
    public class BaseController : Controller
    {
        //
        /// <summary>
        /// Base controller constructor
        /// Handles:
        /// 1) menu and Web.sitemap configuration
        /// ...
        /// </summary>
        /// <returns>void</returns>
        public BaseController()
        {
            if (!SiteMapManager.SiteMaps.ContainsKey("Web"))
            {
                SiteMapManager.SiteMaps.Register<XmlSiteMap>("Web", sitemap => sitemap.LoadFrom("~/Web.sitemap"));
                var siteMap = SiteMapManager.SiteMaps["Web"];
                ViewData["Web"] = siteMap;
            }
        }
    }
}

Now derive from your base controller class:
  namespace Mvc3App.Controllers
  {
      public class HomeController : BaseController
      {

The above works without being on every page, but I assume that it would crash if one did not go to the home controller first.  But now one can add custom auth or logging, etc to this base page.

Phil
Tags
Menu
Asked by
Phil
Top achievements
Rank 2
Share this question
or