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--分页和排序
分页和排序,应该是软件开发中,需要必知必会的技能了,对于分页,网上很多教程,当然,别人终究是别人的,只有自己理解,会了,并且吸收之后,再用自己的语言,传授出来,这才是硬道理.好了,废话说多了.现在我们 ...
随机推荐
- 利用Dockerfile构建一个基于centos 7,包括java 8, tomcat 7,php ,mysql+mycat的镜像
Dockerfile内容如下: FROM centos MAINTAINER Victor ivictor@foxmail.com WORKDIR /root RUN rm -f /etc/yum.r ...
- c 语言的位运算符复习
转载和修正,原文连接:http://www.cnblogs.com/911/archive/2008/05/20/1203477.html 位运算是指按二进制进行的运算.在系统软件中,常常需要处理二进 ...
- 附录B 安装MySql数据库
B.1 卸载旧的MaySql程序 第一步 查找以前是否安装有mysql 使用命令查看是否已经安装过mysql: #rpm -qa | grep -i mysql 如果没有结果,则可以进行mysq ...
- css截断长文本显示
实现 截断长文本显示处理,以前是通过后台的截取,但这种方法容易丢失数据,不利于SEO. 而通过前端css的截断,则灵活多变,可统一运用与整个网站. 这项技术主要运用了text-overflow属性,这 ...
- SQL Server 2016 SP1 标准版等同企业版?!
上周微软发布了SQL Server的历史性公告:SQL Server 标准版的SP1提供你和企业版一样得功能.你不信的话?可以点击这里. 这改变了整个关系数据库市场,重重打击了Oracle.在今天的文 ...
- C#在winform中调用系统控制台输出
在Winform程序中有时候调试会通过Console.Write()方式输出一些信息,这些信息是在Visual Studio的输出窗口显示. 所以就会想,能不能调用系统的Cmd窗口输出呢,经过一番查阅 ...
- ajax配合一般处理程序(.ashx)登录的一般写法
前端: <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> ...
- python处理空格脚本
博客园上传代码时拷贝vs里面的代码不能直接粘贴,否则空格会不符合要求 去掉空格代码 # -*- coding: utf-8 -*- '''打开delSpace.txt文本并删除每行开头的八个空格''' ...
- C#基础-压缩文件及故障排除
C#压缩文件可以使用第三方dll库:ICSharpCode.SharpZipLib.dll: 以下代码能实现文件夹与多个文件的同时压缩.(例:把三个文件夹和五个文件一起压缩成一个zip) 直接上代码, ...
- 第 31 章 项目实战-PC端固定布局[1]
学习要点: 1.准备工作 2.创建项目 3.网站结构 4.CSS选择器 5.完成导航 主讲教师:李炎恢 本章主要开始使用学习用HTML5和CSS3来构建Web页面,第一个项目采用PC端 固定布局来实现 ...