**
本文目标
**
1.能够重用Razor模板进行页面的组件化搭建
**
本文目录
**
1.母板页_Layout.cshtml
2.用户自定义控件
3.默认Layout引用的使用(_ViewStart.cshtml)
1.母板页_Layout.cshtml
类似于传统WebForm中的.master文件,起到页面整体框架重用的目地
**
1.母板页代码预览
**
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>List</title>
</head>
<body>
<div>
<div style="width:100%;height:200px;background-color:thistle;text-align:center;color:honeydew">
@RenderSection("Header")
</div>
<div style="width:100%;height:500px;background-color:tomato;text-align:center;color:aliceblue">
@RenderBody()
</div>
<div style="width:100%;height:200px;background-color:plum;text-align:center;color:dimgray">
@RenderSection("Footer")
</div>
</div>
</body>
</html>
**
子页面主内容的设置
**
页面主内容是由@RenderBody()来标识的。子页面的内容直接替换到该方法处。
@{
ViewBag.Title = "Index";
Layout = "~/Views/List.cshtml";
}
@section Header{
这是头部内容
}
这是身体内容
@section Footer{
这是脚部内容
}
效果如下: