已定义以下部分但尚未为布局页面“〜/ Views / Shared / _Layout.cshtml”呈现:“精选”

时间:2015-06-19 08:41:59

标签: asp.net-mvc

堆栈追踪:

[HttpException (0x80004005): The following sections have been defined but have not been rendered for the layout page "~/Views/Shared/_Layout.cshtml": "featured".]
   System.Web.WebPages.WebPageBase.VerifyRenderedBodyOrSections() +192327
   System.Web.WebPages.WebPageBase.PopContext() +316
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +95
   System.Web.WebPages.<>c__DisplayClass7.<RenderPageCore>b__6(TextWriter writer) +233
   System.Web.WebPages.HelperResult.WriteTo(TextWriter writer) +10
   System.Web.WebPages.WebPageBase.Write(HelperResult result) +71
   System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body) +64
   System.Web.WebPages.WebPageBase.PopContext() +246
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +95
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +260
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +295
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +23
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +242
   System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +21
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177
   System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +89
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +102
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +43
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +57
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +47
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +25
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +47
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9658396
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34249 

4 个答案:

答案 0 :(得分:6)

你必须包括**

  

“@ RenderSection(”featured“,required:false)”

**在_layout.cshtml

的标题部分

答案 1 :(得分:5)

这意味着您已在主 Layout.cshtml 中定义了一个部分,但您的视图中没有为该部分添加任何内容。

如果您的_Layout.cshtml有类似的内容:

@RenderSection("scripts")

然后,使用该布局的所有视图必须包含具有相同名称的@section(即使该部分的内容为空):

@{
    ViewBag.Title = "Title";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section scripts{
    // Add something here
}
  

作为替代方案,您可以将必需设置为false,然后您就不会成功   需要在每个视图中添加该部分,

@RenderSection("scripts", required: false)

或者您也可以将@RenderSection包装在if块中,

@if (IsSectionDefined("scripts"))
{
    RenderSection("scripts");
}

此外,您可以将以下行添加到_Layout.cshtml或_ Layout.Mobile.cshtml

  

@RenderSection(&#34;脚本&#34;,必需:false)

答案 2 :(得分:1)

如果您使用@section scripts {}或@section styles {},则必须在布局页面中的标题或正文中添加以下代码  @RenderSection(&#34;脚本&#34;,必需:false)  @RenderSection(&#34; styles&#34;,required:false)

答案 3 :(得分:0)

当您将_Layout.cshtml文件的Layout属性设置为其自身时,也会引发此错误。例如,通过_ViewStart.cshtml文件中的以下代码:

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