Layout views hold the common markup shared between views such as the Doctype and head tag. Layout views are typically not returned as a view result for controllers and by convention, like any other non-primary content view, their names begin with an underscore. By default, there is a _Layout.cshtml file inside the Views > Shared folder.
Two Special Methods
There are two special methods that layout views can use.
@RenderBody(): (Not Optional) Wherever this is placed in the layout view is where the content view will be inserted.
@RenderSection("sectionName", required:false): (Optional) You can have more than one of these in the layout view. "sectionName" can be anything you want. Since required is set to false, you can choose which views the section will show up in.
To have a section appear in a particular view, like say Index.cshtml, just add code like the one shown below anywhere in your content view.
// (Index.cshtml) @section featured { This is some featured content. }
It will be inserted wherever @RenderSection("featured", required:false) is placed in the _Layout.cshtml.
Specifying the Layout View
MVC knows which layout view to use as it is specified in a file called _ViewStart.cshtml inside the Views folder. Code inside this file will execute before your view starts rendering and sets the Layout property.
Since the _ViewStart.cshtml is in the root of the Views folder, it will apply to all views that are inside the Views folder. You can change which file to use by changing the path string assigned to the Layout property.
Changing the Layout View for Subfolders
You can specify a different layout file to use in subfolders of the Views folder. For example, say you have a subfolder called Reviews inside of Views. If you make another _ViewStart.cshtml file, assign a different path string for the Layout property, and place the file inside the Reviews folder, it will override the Layout property of the _ViewStart.cshtml inside the root of the Views folder. This layout view will apply to all the views inside the Reviews folder.
Changing the Layout View for a Single View
You can specify the layout file inside a particular view as well so it only applies to one view. To do this, you can set the Layout property at the top of the view.
Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
✓ Live Streaming✓ Interactive Chat✓ Private Shows✓ HD Quality
Anya is LIVE right now
FREE
Free to watch • No registration required • HD streaming