MVCSiteMapProvider is causing an issue when I add an extra parameter to my service constructor

943 Views Asked by At

Hi this is really weird I am using MVCSiteMapProvider for building a navigation bar. I have a service called Code Serice

public CodeService(
       ICodeRepository codeRepository,
       ICodeStageRepository codeStageRepository,
       ICodeListRepository codeListRepository,
       ICookieService cookieService)
   {
       this.codeRepository = codeRepository;
       this.codeListRepository = codeListRepository;
       this.cookieService = cookieService;
       this.codeStageRepository = codeStageRepository;
   }

if I add an extra Interface to this constructor, my project builds but my entire app crashes when I run it with the following error. enter image description here

My Entire Layout is

@using System.Web.Optimization
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title - ReferenceDataManagement </title>
    @Styles.Render("~/content/css/MasterCss")
    @Styles.Render("~/content/jquery.jqGrid/jqgridcss")
    @Styles.Render("~/content/themes/base/jqueryuicss")

</head>
<body>
    <div class="container-fluid">
        <div class="row-fluid"> @RenderPage("~/Views/Shared/_Header.cshtml") </div>
        <div class="row-fluid"> @Html.MvcSiteMap().Menu("BootstrapMenuHelperModel") </div>
        <div class="row-fluid"> @RenderBody() </div>
        <div class="row-fluid"> @RenderPage("~/Views/Shared/_Footer.cshtml") </div>
    </div>
    @Scripts.Render("~/Scripts/MasterScripts")    
    @RenderSection("scriptholder", false)
</body>
</html>

And Bootstrap menu helper is as follows

@model MenuHelperModel

@helper  TopMenu(List<SiteMapNodeModel> nodeList)
{
@:
<div class="navbar">
    <div class="navbar-inner">
        <div class="container">

            <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </a>
            <a class="brand hidden-desktop" href="#">RDM</a>

            <div class="nav-collapse collapse">
                <ul class="nav">
                    @foreach (var node in nodeList)
                    {
                        var current = String.Empty;

                        var url = node.IsClickable ? node.Url : "#";

                        if (node.IsCurrentNode)
                        {
                            current = "active";
                        }

                        if (!node.Children.Any())
                        {
                            @:<li class="@current"><a href="@url">@node.Title</a></li>
                        }
                        else
                        {
                            foreach (var child in node.Children)
                            {
                                if (child.IsCurrentNode)
                                {
                                    current = "active";  
                                }
                            }

                            @:<li class="dropdown @current"><a class="dropdown-toggle" data-toggle="dropdown" href="@url">@node.Title <b class="caret"></b></a>@DropDownMenu(node.Children)</li>
                        }

                        if (node != nodeList.Last())
                        {
                            @:<li class="divider-vertical"></li>
                        }
                    }
                </ul>

                <ul class="nav pull-right">
                    <li class="divider">
                        @Html.ActionLink("Sign Out", "SignOutConfirmation", "Account") 
                    </li>
                </ul> 
            </div>
        </div>
    </div>
</div>
}

@helper DropDownMenu(SiteMapNodeModelList nodeList)
{
  <ul class="dropdown-menu">
  @foreach (var node in nodeList)
  {
    if (node.Title == "Separator")
    {
      @:<li class="divider"></li>
      continue;
    }

    var url = node.IsClickable ? node.Url : "#";

    if (!node.Children.Any())
    {
      @:<li><a href="@url">@node.Title</a></li>
    }
    else
    {
       @:<li class="dropdown-submenu"><a href="@url">@node.Title</a>@DropDownMenu(node.Children)</li>
    }
  }
  </ul>
}

   @TopMenu(Model.Nodes)

I can't see how these pieces of code are related to each other or where a conflict can occur. Has anyone come across a similar issue?

EDIT:

<?xml version="1.0" encoding="utf-8" ?>
<mvcSiteMap xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-4.0"
        xsi:schemaLocation="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-4.0 MvcSiteMapSchema.xsd">

  <mvcSiteMapNode title="Home" controller="Home" action="Index">
     <mvcSiteMapNode title="About" controller="Home" action="About"/>
     <mvcSiteMapNode title="Contact" controller="Home" action="Contact"/>
     <mvcSiteMapNode title="Context" controller="Context" action="Index"/>
     <mvcSiteMapNode title="Code List" controller="CodeList" action="Index" roles="Administrator" />
     <mvcSiteMapNode title="Code" controller="Code" action="Index" roles="Administrator" />

   <mvcSiteMapNode title="Users" controller ="" clickable="false" roles="Administrator, ContextOwner">
      <mvcSiteMapNode title="Application Users" controller="User" action="Index" roles="ContextOwner"/>
      <mvcSiteMapNode title="Maintain Context Users" controller="ContextUser" action="Index"/>
   </mvcSiteMapNode>

   <mvcSiteMapNode title="Admin" clickable="false" controller="UploadXmlSchema" roles="Administrator">
      <mvcSiteMapNode title="XML Schema" controller="UploadXmlSchema" action="Index" />
    </mvcSiteMapNode>

    <mvcSiteMapNode title="Approvals" controller="Approval" action="Index" />
  </mvcSiteMapNode>
</mvcSiteMap>
1

There are 1 best solutions below

1
On BEST ANSWER

If I am not mistaken, this is a bug that was fixed in v4.4.6. The problem is caused by setting the controller property to null and then reading back the property (which is what the Menu HTML helper will do internally).

If so, you can either upgrade to v4.4.6 to fix the issue or just ensure that your Controller property (or the "controller" key of the RouteValues property) is always set to an empty string instead of null.

node.Controller = theValue ?? string.Empty;