当我向服务构造函数添加额外参数时,MVCSiteMapProvider会导致问题

时间:2013-12-11 12:26:46

标签: asp.net-mvc-4 mvcsitemapprovider

嗨,这真的很奇怪我使用MVCSiteMapProvider来构建导航栏。我有一个名为Code Serice的服务

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

如果我为这个构造函数添加一个额外的接口,我的项目会构建,但是当我运行它时,我的整个应用程序崩溃并出现以下错误。 enter image description here

我的整个布局

@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>

Bootstrap菜单助手如下

@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)

我无法看到这些代码如何相互关联或可能发生冲突的地方。有没有人遇到类似的问题?

修改

<?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 个答案:

答案 0 :(得分:1)

如果我没有记错,这是v4.4.6中修复的错误。问题是由于将controller属性设置为null然后读回属性(这是Menu HTML帮助程序将在内部执行的操作)引起的。

如果是这样,您可以升级到v4.4.6来解决问题,或者只是确保您的Controller属性(或RouteValues属性的“controller”键)始终设置为空字符串而不是null。

node.Controller = theValue ?? string.Empty;