Is there a way to get a list of sections defined in a layout file? For example, if I want to know what sections are defined in my Shared/_Layout.cshtml file is there a way to parse that Layout file so that I know what sections exist in the layout?
Share
There’s no built-in function I’m aware of because the name isn’t necessarily known without executing the view.
You can probably just run a regular expression over your layouts, like
which accepts @RenderSection(“foo”) or @RenderSection( “foo” ), but skips @@RenderSection (the @@ escaped @).
However, this assumes that the name of the section is passed in as a string literal. The view could also look like (not your typical situation, but possible):
In that case you’re pretty much lost.