.Net Core · C#

Sections

Sections allow to define where certain page content should be located on layout. They can be optional or required. In case section is marked as required and it is not found, an exception will be thrown.
To add a new section, go to the layout page and add:

 @RenderSection("MySection", required: true)

RenderSection takes two parameters: section name and bool value that indicates if section is required or not.
By using IsSectionDefined syntax you can find out if section is defined and make some actions based on this:

@if (IsSectionDefined("MySection")) { 
    @RenderSection("MySection")
}

Inside the view, where you use layout from the previous step add the following code:

@section MySection
{
    <p>Me section</p>
}

Leave a Reply

Your email address will not be published. Required fields are marked *