MVC 9 Site Layout
MVC 9 Site Layout
MVC 9 Site Layout
The final step for using Bootstrap 4 in your ASP.NET MVC project is to create a Layout
page that will contain all the necessary CSS and JavaScript files in order to include
Bootstrap components in your pages. To create a Layout page, follow these steps:
2. Add a new MVC View Layout Page to the Shared folder. The item can be
found in the .NET Core | Server-side category of the Add New Item dialog.
<script src="~/lib/jquery/dist/jquery.js"></script>
...
Another example
You need to create a new empty ASP.NET MVC 5 project, and added a new
folder Shared in View and new file _Layout.cshtml and put this code to that (for
example )
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>@ViewBag.Title</title>
<script src="@Url.Content("~/Content/style.css")"></script>
</head>
<body>
@RenderBody()
</body>
</html>
and now when I add new view for a controller it doesn't detect it as master page
automatically as well as Basic Template
@{
ViewBag.Title = "AboutUs";
}
<h2>AboutUs</h2>
it doesnt get _Layout.cshtml file as master page until I set that manually with Layout
= "~/Views/Shared/_Layout.cshtml";
you need a _ViewStart.cshtml in your Views folder which should have the following
in it:
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
The layout view is same as the master page of the ASP.NET
webform application.
For example, an application UI may contain Header, Left menu bar,
right bar and footer section that remains same in every page and
only the centre section changes dynamically as shown below.
The layout view allows you to define a common site template, which
can be inherited in multiple views to provide a consistent look and
feel in multiple pages of an application. The layout view eliminates
duplicate coding and enhances development speed and easy
maintenance. The layout view for the above sample UI would
contain a Header, Left Menu, Right bar and Footer sections. It
contains a placeholder for the center section that changes
dynamically as shown below.
Layout View
The razor layout view has same extension as other views, .cshtml
or .vbhtml. Layout views are shared with multiple views, so it must
be stored in the Shared folder. For example, when we created
our first MVC application in the previous section, it also created
_Layout.cshtml in the Shared folder as shown below.
_Layout.cshtml:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-
toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@Html.ActionLink("Application name", "Index", "Home", new {
area = "" }, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
</div>
</div>
</div>
<div class="container body-content">
@RenderBody()
<hr />
<footer>
<p>© @DateTime.Now.Year - My ASP.NET Application</p>
</footer>
</div>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
</body>
</html>
As you can see, the layout view contains html Doctype, head and
body as normal html, the only difference is call to RenderBody() and
RenderSection() methods. RenderBody acts like a placeholder for
other views. For example, Index.cshtml in the home folder will be
injected and rendered in the layout view, where the RenderBody()
method is being called. You will learn about these rendering
methods later in this section.
_ViewStart.cshtml
_ViewStart.cshtml
Index View:
@{
ViewBag.Title = "Home Page";
Layout = "~/Views/Shared/_myLayoutPage.cshtml";
}
<div class="jumbotron">
<h1>ASP.NET</h1>
<p class="lead">ASP.NET is a free web framework for building great Web
sites and Web applications using HTML, CSS and JavaScript.</p>
<p><a href="http://asp.net" class="btn btn-primary btn-lg">Learn more
»</a></p>
</div>
<div class="row">
<div class="col-md-4">
<h2>Getting started</h2>
<p>
ASP.NET MVC gives you a powerful, patterns-based way to build
dynamic websites that
enables a clean separation of concerns and gives you full
control over markup
for enjoyable, agile development.
</p>
<p><a class="btn btn-default"
href="http://go.microsoft.com/fwlink/?LinkId=301865">Learn more
»</a></p>
</div>
<div class="col-md-4">
<h2>Get more libraries</h2>
<p>NuGet is a free Visual Studio extension that makes it easy to
add, remove, and update libraries and tools in Visual Studio projects.</p>
<p><a class="btn btn-default"
href="http://go.microsoft.com/fwlink/?LinkId=301866">Learn more
»</a></p>
</div>
<div class="col-md-4">
<h2>Web Hosting</h2>
<p>You can easily find a web hosting company that offers the right
mix of features and price for your applications.</p>
<p><a class="btn btn-default"
href="http://go.microsoft.com/fwlink/?LinkId=301867">Learn more
»</a></p>
</div>
</div>
Basically IF we have master layout in Views>> Shared >> _Layout.cshtml and this
thing is defined in _ViewStart.cshtml that which one is our default master layout.
When we create a view with master layout by default its master layout
is _Layout.cshtml, but we can change it from _ViewStart.cshtml
When we write:
@{
Layout = null;
}
in our view we say that this view does not have any master layout, this is used
when we create partial view mostly or a standalone view without master layout.
You can also change of some specific view Master Layout by writing on top of it
the url of master layout view:
@{
Layout = "~/Views/Shared/_CustomMasterLayout.cshtml";
}