The following sections have been defined but have not been rendered for the layout page

前端 未结 5 823
故里飘歌
故里飘歌 2021-02-03 18:13

This is an ASP.NET MVC 3 exception message. What it says? What should I do?

OK, I have this code:

@{
     Layout = \"~/_Layout.cshtml\";
     Page.Title          


        
相关标签:
5条回答
  • 2021-02-03 18:24

    The error message implies that your _Layout.cshtml does not include @RenderSection statements for the @sections that you have in your view.

    Check your Layout and let us know.

    See this page for more information.

    0 讨论(0)
  • 2021-02-03 18:24

    When I received this error I was trying to conditionally include a section in my layout when it was defined by the view. Here is the layout:

    @if (IsSectionDefined("header"))
    {
        RenderSection("header");
    }
    else
    {
        Html.RenderPartial("_Header");
    }
    

    I think @Jaider mentions this in his answer too, but you cannot place a RenderSection inside an if statement. The way to achieve my goal was to inverse the if and set the required parameter to false on the RenderSection method:

    @RenderSection("header", false);
    @if (!IsSectionDefined("header"))
    {
        Html.RenderPartial("_Header");
    }
    

    If no setion is defined, the first line wont do anything and the If statement ensures a default header is rendered when no section is defined.

    0 讨论(0)
  • 2021-02-03 18:25

    I faced a similar problem where the layout template indeed has the @RenderSection(...) but inside an if-else statement. So when the page executes the statement that doesn't contain the @RenderSection it will throw that exception. If that is your case, then your solution is a little more complicated, either:

    • make sure you have @RenderSection outside of the statements or
    • repeat @RenderSection in both if-else statements, or
    • use different partial views, or
    • redesign the layout.

    That could be your main problem!

    0 讨论(0)
  • 2021-02-03 18:30

    usually this error occurs if you do not write code for the section which is required by your master layout-page

    Reference: https://ziaahmedshaikh.wordpress.com/2016/02/17/the-following-sections-have-been-defined-but-have-not-been-rendered-for-the-layout-page/

    0 讨论(0)
  • 2021-02-03 18:38

    Your layout page isn't actually rendering the sections footer and meta

    In your _Layout.cshtml, put in @RenderSection("meta") where you want the meta section rendered.

    You can also do @RenderSection("meta", false) to indicate that the section is optional.

    0 讨论(0)
提交回复
热议问题