MVC学习系列5--Layout布局页和RenderSection的使用
我们开发网站项目的时候,都会遇到这样的问题:就是页面怎么统一风格,有一致的外观,在之前ASP.NET的时代,我们有两种选择,一个是使用MasterPage页,一个是手动,自己在每个页面写CSS样式,但这样代码量太大了。。不可取,那么到了ASP.NET MVC时代,有什么技术可以统一页面风格呢???有,那就是Layout布局视图。下面就开始学习吧。
1. 首先使用空模板,新建一个MVC Web项目:

新建完成之后,初始化状态是:

2.接着在根目录【LayoutMVC这里是】下,新建一个文件夹【Content】,在里面添加一个css文件,取名【Site.css】

3.在【Views】文件夹下,新建一个【Shared】文件夹,在Shared文件夹下,新建一个Layout布局页

布局页:
<!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
@*Url.Content将虚拟路径转化为应用程序的绝对路径 *@
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" />
</head>
<body>
<header>
<div class="content-wrapper">
<div class="float-left">
<p class="site-title">
@Html.ActionLink("Code Express", "Index", "Home")
</p>
</div>
<div class="float-right">
<nav>
<ul id="menu">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
</ul>
</nav>
</div>
</div>
</header>
<div id="body">
@RenderSection("featured", required: false)
<section class="content-wrapper main-content clear-fix">
@RenderBody()
</section>
</div>
<footer>
<div class="content-wrapper">
<div class="float-left">
<p>© @DateTime.Now.Year – Code Express</p>
</div>
</div>
</footer>
</body>
</html>In this layout we are using a HTML helper method and some other system defined methods so let's see these methods one by one. 在布局页里面,我们使用了HTML帮助类的方法,和其他一些系统定义的方法,我们来分别看看这些方法。
Url.Content(): Content() method is a method of UrlHelper class. It converts a virtual (relative) path to an application absolute path.
It has one parameter of string type that is a virtual path of the content.
It returns an application's absolute path.
If the specified content path (parameter of the method) does not start with the tilde (~) character then this method returns contentPath unchanged.
Url.Content() ensures that all links work no matter if the site is in a virtual directory or in the web site root. URL。Content方法,是UrlHelper类中的方法,它可以把虚拟【相对】路径转化为应用程序的绝对路径,它有一个string类型的参数,也就是文件的虚拟路径。如果这个路径没有以~波浪线开头,然后这个方法就会返回一个固定路径,
它确保所有的链接都能正常工作,不管站点是在虚拟路径中或者是在网站的根目录下。 Html.ActionLink(): The easiest way to render an HTML link in is to use the HTML.ActionLink() helper.
With MVC, the Html.ActionLink() does not link to a view.
It creates a link to a controller action. ActionLink() is an extension method of the HtmlHelper class.
It returns an anchor element (an element) that contains the virtual path of the specified action.
When you use an ActionLink() method then you need to pass three string parameter.
The parameters are linkText (the inner text of the anchor element), actionName (the name of the action) and controllerName (the name of the controller). HTML.ActionLink方法,这是最简单的方法,来做一个HTML链接。在MVC中HTML.ActionLink不是链接到视图,而是链接到控制器的方法,ActionLink是HTMLHelper类的扩展方法。
它返回了一个包含指定Action的虚拟路径的链接。使用ActionLink需要传递3个参数,第一个是链接显示的文本,第二个是要链接的控制器方法,第三个是控制器的名字。 RenderSection(): RenderSection() is a method of the WebPageBase class.
Scott wrote at one point, The first parameter to the "RenderSection()" helper method specifies the name of the section we want to render at that location in the layout template.
The second parameter is optional, and allows us to define whether the section we are rendering is required or not.
If a section is "required", then Razor will throw an error at runtime
if that section is not implemented within a view template that is based on the layout file (that can make it easier to track down content errors). It returns the HTML content to render. RenderSection是WebPageBase类中的方法,我们可以在布局页中使用它来,作为一个占位符,就和ASP.NET中类似,有两个参数,一个是名字,一个是Required,Required设置为True的时候,我们在视图中就一定要添加这个块,否则运行的时候,报错。 RenderBody(): In layout pages, renders the portion of a content page that is not within a named section. It returns the HTML content to render. RenderBody is required, since it's what renders each view. RenderBody是加载显示,不在RendSection代码快中的内容,RenderBody是必须要的。 The _ViewStart File _ViewStart文件,指定了使用的布局页,如果没有的话,你就需要在每个视图中手动,添加
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
The "_ViewStart" file in the Views folder contains the following content: @{ Layout = "~/Views/Shared/_Layout.cshtml"; }
This code is automatically added to all views displayed by the application. If you remove this file then you must add this line to all views.
4.接着,我们新建一个控制器Home,创建对应的Index视图,指定我们刚才创建的布局页
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
}
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
5.运行程序:

MVC学习系列5--Layout布局页和RenderSection的使用的更多相关文章
- MVC之LayOut布局页
LayOut布局页,就是相当于WebForm中母版页,实现了一个代码的共用和公共布局的作用. 布局页的使用 (1)添加新项,选择MVC布局页 <!DOCTYPE html> <htm ...
- MVC学习系列4--@helper辅助方法和用户自定义HTML方法
在HTML Helper,帮助类的帮助下,我们可以动态的创建HTML控件.HTML帮助类是在视图中,用来呈现HTML内容的.HTML帮助类是一个方法,它返回的是string类型的值. HTML帮助类, ...
- ASP.NET MVC学习系列(二)-WebAPI请求
继续接着上文 ASP.NET MVC学习系列(一)-WebAPI初探 来看看对于一般前台页面发起的get和post请求,我们在Web API中要如何来处理. 这里我使用Jquery 来发起异步请求实现 ...
- ASP.NET MVC学习系列(二)-WebAPI请求(转)
转自:http://www.cnblogs.com/babycool/p/3922738.html 继续接着上文 ASP.NET MVC学习系列(一)-WebAPI初探 来看看对于一般前台页面发起的g ...
- MVC学习系列——ModelBinder扩展
在MVC系统中,我们接受数据,运用的是ModelBinder 的技术. MVC学习系列——ActionResult扩展在这个系列中,我们自定义了XmlResult的返回结果. 那么是不是意味着能POS ...
- MVC学习系列——记一次失败面试后,感想。
在此写博客之际,热烈庆祝母校苏州科技学院,正式改名为苏州科技大学. 一晃眼,从自己投身IT行业已经两年有余,期间经历了结婚.买房等人生大事,非常感谢我的老婆,谢谢她这么爱我,嫁给我这个码农,呵呵... ...
- [转]ASP.NET MVC学习系列(二)-WebAPI请求 传参
[转]ASP.NET MVC学习系列(二)-WebAPI请求 传参 本文转自:http://www.cnblogs.com/babycool/p/3922738.html ASP.NET MVC学习系 ...
- ASP.NET MVC学习系列(二)-WebAPI请求 转载https://www.cnblogs.com/babycool/p/3922738.html
继续接着上文 ASP.NET MVC学习系列(一)-WebAPI初探 来看看对于一般前台页面发起的get和post请求,我们在Web API中要如何来处理. 这里我使用Jquery 来发起异步请求实现 ...
- MVC学习系列8--分页和排序
分页和排序,应该是软件开发中,需要必知必会的技能了,对于分页,网上很多教程,当然,别人终究是别人的,只有自己理解,会了,并且吸收之后,再用自己的语言,传授出来,这才是硬道理.好了,废话说多了.现在我们 ...
随机推荐
- scikit-learn 逻辑回归类库使用小结
之前在逻辑回归原理小结这篇文章中,对逻辑回归的原理做了小结.这里接着对scikit-learn中逻辑回归类库的我的使用经验做一个总结.重点讲述调参中要注意的事项. 1. 概述 在scikit-lear ...
- jquery EasyUI的formatter格式化函数代码
要格式化数据表格列,需要设置formatter属性,该属性是一个函数,它包含两个参数: value: 对应字段的当前列的值 record: 当前行的记录数据 复制代码 代码如下: $('#tt' ...
- EF Code First 一对多、多对多关联,如何加载子集合?
应用场景 先简单描述一下标题的意思:使用 EF Code First 映射配置 Entity 之间的关系,可能是一对多关系,也可能是多对多关系,那如何加载 Entity 下关联的 ICollectio ...
- Int,Long比较重使用equal替换==
首先,==有很多限制,如Integer 类型的值在[-128,127] 期间,Integer 用 “==”是可以的(参考),超过范围则不行,那么使用equal则代替则完全ok public stati ...
- (转载)基于LBS地图的开发,满足地图上有头像的需求
最近做的项目主要是LBS这块 主打成员定位功能 我们的UI设计是这样的 乍一看上去是挺好挺美观的 不同的人会显示不同的头像 可是当人扎堆的时候 问题就来了 当人多的时候(例如上图所示) 地图滑动起来就 ...
- AppCan学习笔记----Request和登录功能简单实现
appcan.ajax(options) 实现appcan中网络数据的上传和获取 发起一个ajax请求,并获取相应的内容 常用参数 options.type: 请求的类型,包括GET.POST等 op ...
- 用浏览器(支持WebSocket)和node-inspector 调试后端(CoffeeScript,Typescript)代码
调试效果 配置 npm安装node-inspector: $ npm install -g node-inspector 配置gulp,gulp可以用 gulp-node-inspector 或 用g ...
- html5数字和颜色输入框
html5功能强大,数字和颜色输入框例子 效果:http://hovertree.com/code/html5/rxujb6g8.htm <!DOCTYPE html> <html& ...
- PHP intval()
定义和用法 获取变量的整数值,允许以使用特定的进制返回.默认10进制 注:如果参数为整数,则不做任何处理. 语法 intval (var, base) 参数 描述 var 必须.可以是任何标量类型. ...
- Struts2基于注解的Action配置
使用注解来配置Action的最大好处就是可以实现零配置,但是事务都是有利有弊的,使用方便,维护起来就没那么方便了. 要使用注解方式,我们必须添加一个额外包:struts2-convention-plu ...