10

我在 ASP.NET MVC 4 中有一个多级布局母版页。

我有以下内容:

<title>@ViewBag.Title</title>

布局页面的顺序如下:

  1. _Layout.cshtml
  2. _SubLayout.cshtml(基于 _Layout.cshtml)
  3. Index.cshtml(基于_SubLayout.cshtml)

我在索引操作中设置@ViewBag.Title。但是,我得到以下异常:

值不能为 null 或空。参数名称:contentPath


这是我的代码。我只是让 ASP.NET 设计模式书籍的代码适用于 VS 2012 / MVC 4

_Layout.cshtml

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title</title>
    <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <meta name="viewport" content="width=device-width" />
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />  
</head>
<body>  
<div id="main">    
    <div id="header">        
        <span><a href="@Url.Content("")">
        <img alt="Agatha's Clothing Store" 
             src="@Url.Content("/Content/Images/Structure/lg_logo.png")" 
             border="0" /></a></span>        
    </div>   

    <div id="headerSummary">        
         @RenderSection("headerBasketSummary", required: false)                   
    </div>
    <div class="topBarContainer">          
        <div id="background">         
            <div id="navigation">
                @RenderSection("MenuContent", required: false)
            </div>    
            <div id="content">
               @RenderBody()
            </div>            
            <div style="clear: both;" />
        </div>  
    </div>          
    @Html.Partial("_SiteFooter")
</div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/jquery-ui.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/json2/20121008/json2.js"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-jtemplates.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/agatha-common-scripts.js")"></script>

</body>
</html>

_ProductCatalogProduct.cshtml

@model BaseProductCatalogPageView
@using Agathas.Storefront.Controllers.ViewModels.ProductCatalog
@using Agathas.Storefront.UI.Web.MVC.Helpers

@{
    Layout = "~/Views/Shared/_Layout.cshtml";                  
}

@if (IsSectionDefined("MenuContent"))
{
    @RenderSection("MenuContent", required: false)
}
else
{
    @Html.Partial("_Categories", ((BaseProductCatalogPageView)Model).Categories)
}
@RenderBody(); 

索引.cshtml

@model HomePageView
@using Agathas.Storefront.Controllers.ViewModels.ProductCatalog
@using Agathas.Storefront.Services.ViewModels
@using Agathas.Storefront.UI.Web.MVC.Helpers 

@{ 
    Layout = "~/Views/Shared/_ProductCatalogLayout.cshtml";
    ViewBag.Title = "Home Page";
}

<img width="559" height="297" 
        src="@Url.Content("~/Content/Images/Products/product-lifestyle.jpg")"
        style="border-width: 0px; padding: 0px; margin: 0px" />
    <div style="clear: both;"></div>
    <h2>Top Products</h2>
    <div id="items" style="border-width: 1px; padding: 0px; margin: 0px">
        <ul class="items-list">
            @foreach (ProductSummaryView product in Model.Products)
            {
                <li class="item-detail">
                <a class="item-productimage-link" href="@Url.Action("Detail", "Product",     new { id = product.Id }, null)">
            <img class="item-productimage" src="@Url.Content("~/Content/Images/Products/" + product.Id.ToString() +".jpg"))" /></a>
            <div class="item-productname">@Html.ActionLink(product.BrandName + " " + product.Name, "Detail", "Product", new { id = product.Id }, null)</div>
            <div class="item-price">@product.Price</div>
            </li>
        }
    </ul>
</div>

非常感谢

4

3 回答 3

23

这段代码抛出异常:<a href="@Url.Content("")">. 发生这种情况是因为Url.Content方法的 contentPath 参数不能为 null 或为空。

于 2013-08-13T11:42:49.107 回答
0

如果您在子 Razor 视图文件中分配一个值,该值也在父视图文件中设置,但显示在 Layout.cshtml 调用的部分中,则可能会发生这种情况

例如

_Layout.cshtml | (父母)| 父.cshtml | (父母)| Page1.cshtml

在 Page1.cshtml 我做

ViewBag.Title= "Value1";

在 Parent.cshtml 我做同样的事情,但使用 Umbraco 调用:

ViewBag.Title = Umbraco.Field("value").ToString();

解决方法是删除 Parent.cshtml 中的分配。这似乎是一个错误,恐怕这是我找到的唯一修复。希望对你有所帮助...

于 2013-04-15T09:36:41.480 回答
0

在将其用作图像源之前,您应该检查路径是否不为空或为空

   @foreach (var post in Model)
        {

            <div class="col-md-6">
                <div class="product-list post-list">
                    @if (!string.IsNullOrWhiteSpace(post.Photo))
                    {
                        <a href="#" class="product-list-img">
                            <img src="@Url.Content(post.Photo)" alt="">
                        </a>
                    }
</div>
</div>
于 2019-04-17T16:33:07.640 回答