Layout - Tarostar/SimpleNetBlog GitHub Wiki
Add a layout like this:
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
This layout is in a new Shared folder and named _Layout.cshtml and could for example be a navigation menu. Use @RenderBody() to insert the view using the layout into the page. Also remove all the duplicated HTML formatting such as DOCTYPE, html, head and body from the views using the layout as this is now handled by the layout.
By adding a _ViewStart.cshtml directly under the Views folder we can inject any c# code before the views are loaded. We can use this to inject the same Layout for all views by simply adding the Layout = "~/Views/Shared/_Layout.cshtml";
and so we can remove the Layout from all the other views. We could of course still overwrite the layout for any particular view by adding it back in.
By putting e.g. @RenderSection("Scripts", false)
in the layout we can control where it is placed, in this case we could put it at the end to ensure scripts are placed at the bottom of the page.
In the view this is used like this:
@section script { <script>...</script> }
The false parameter means it is not required.