How can I know if a non-required RenderSection exists?

C#asp.netasp.net Mvcasp.net Mvc-3Razor

C# Problem Overview


@* Omitted code.. *@
@RenderBody()
@RenderSection("Sidebar", required: false)

Is there any way to know in the Omitted code part if the RenderSection Sidebar exists or not?

C# Solutions


Solution 1 - C#

@if (IsSectionDefined("Sidebar"))
{
    @RenderSection("Sidebar")
}
else
{
    <div>Some default content</div>
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThomas BoniniView Question on Stackoverflow
Solution 1 - C#Darin DimitrovView Answer on Stackoverflow