通常,您应该为匿名用户和登录用户(页面)设置不同的布局。这将使为具有不同用户场景的页面构建模板变得更加容易,而不是在一个布局中操纵它们。
使用不同的布局,您可以呈现所需的样式表、脚本和HTML部分。
_布局匿名.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="container body-content">
@RenderBody()
</div>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
</body>
</html>
_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>
<!-- Nav section -->
<div class="container body-content">
@RenderBody()
<hr />
<!-- Footer section -->
</div>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
</body>
</html>
在您的登录和注册页面上,您可以指定使用的布局:
@{
Layout = "~/Views/Shared/_LayoutAnonymous.cshtml";
}
而对于仅在登录后才能访问的另一个页面,您可以将布局指定为:
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
参考:
What is Layout View in ASP.NET MVC