mvc5html5模板,MVC|Razor布局-模板页|ViewStart.cshtml

ViewStart.cshtml视图页面

要点1

MVC中执行任何一个视图之前都要先执行Views文件夹下的 _ViewStart.cshtml

视图的内容

注意点:想要上面的描述成立,则在action方法中必须以return View()来返回视图。

如果是以PowerView()

方法来返回视图的话,则不会执行_ViewStart.cshtml 中的内容

public class HomeController : Controller

{

public ActionResult Index()

{

//MVC中执行任何一个视图之前都要先执行Views文件夹下的_ViewStart.cshtml视图的内容

// ↓↓

return View(); //但是只有以return

View()的形式返回视图,才会执行_ViewStart.cshtml视图中的内容

//return PartialView(); 如果如果是以PowerView()

方法来返回视图的话,则不会执行_ViewStart.cshtml 中的内容

}

}

}

要点2

ViewStart.cshtml视图一般是直接存在与Views视图下面的。当然其他文件夹下面也是可以有ViewStart.cshtml视图页面的

(前提是这个文件夹是在Views文件夹下)

我们看到在Views文件夹下存在ViewStart.cshtml文件外,在Home文件夹下同样也存在一个ViewStart.cshtml文件。

那么当存在多个ViewStart.cshtml文件的时候执行顺序是什么呢?

答:

1>首先是执行Views文件夹下的ViewStart.cshtml视图文件的内容。当我们访问的是Home文件夹下的的视图的时候,Home文件夹下的ViewStart.cshtml文件才会被执行。

也就是先执行了Views文件夹下的ViewStart.cshtml视图文件内容,后才执行Home文件夹下的ViewStart.cshtml视图内容

2>当我们访问的是Test文件下的视图的时候,就仅仅是执行Views文件夹下的ViewStart.cshtml视图内容(因为Test文件夹下不存在ViewStart.cshtml视图文件)

a4c26d1e5885305701be709a3d33442f.png

_ViewStart.cshtml

视图文件的作用

_ViewStart.cshtml

文件的作用:一般是用于存放MVC网站中所有视图公用的js,css等文件。这样就不需要在每个视图中都拖一个js,css等文件进来了

模板页

_Layout.cshtml

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">html>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">head>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break: break-all;">meta

charset="utf-8"

/>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break: break-all;">meta

name="viewport"

content="width=device-width"

/>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">title>@ViewBag.Title

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">title>

@Styles.Render("~/Content/css")

@Scripts.Render("~/bundles/jquery")

@RenderSection("scripts", required: false)

@RenderSection("Mycss",required:true)

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">head>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">body>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break: break-all;">div

style="width:800px;margin:0px

auto">

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break: break-all;">div

style="border:1px

solid red; height:50px">顶部内容

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">div>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">div>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break: break-all;">div

style="float:left;

width:150px; height:500px; border:1px solid blue" >

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">ul>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">li>菜单1

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">li>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">li>菜单2

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">li>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">li>菜单3

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">li>

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">ul>

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">div>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break: break-all;">div

style="float:

right; width: 640px; height: 500px; border: 1px solid

blue">

@RenderBody()

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">div>

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">div>

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">div>

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">body>

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">html>

在看看我们继承这个模板页的子页面 (Home控制器下的Index视图,它使用了上面这个模板页)

@{

ViewBag.Title = "Index";

}

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">h2>Index

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">h2>

@section scripts{

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break: break-all;">script

type="text/javascript">

alert("你好");

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">script>

}

@section Mycss{

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break: break-all;">style

type="text/css">

* {

color:red;

}

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">style>

}

a4c26d1e5885305701be709a3d33442f.png

HTML

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">html>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">head>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break: break-all;">meta

charset="utf-8"

/>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break: break-all;">meta

name="viewport"

content="width=device-width"

/>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">title>Index

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">title>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break: break-all;">link

href="/Content/site.css"

rel="stylesheet"/>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break: break-all;">script

src="/Scripts/jquery-1.8.2.js">

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">script>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break: break-all;">script

type="text/javascript">

alert("你好");

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">script>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break: break-all;">style

type="text/css">

* {

color:red;

}

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">style>

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">head>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">body>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break: break-all;">div

style="width:800px;margin:0px

auto">

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break: break-all;">div

style="border:1px

solid red; height:50px">顶部内容

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">div>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">div>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break: break-all;">div

style="float:left;

width:150px; height:500px; border:1px solid blue" >

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">ul>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">li>菜单1

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">li>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">li>菜单2

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">li>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">li>菜单3

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">li>

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">ul>

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">div>

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break: break-all;">div

style="float:

right; width: 640px; height: 500px; border: 1px solid

blue">

<

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">h2>Index

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">h2>

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">div>

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">div>

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">div>

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">body>

class="hljs-name" style="box-sizing: border-box; outline: 0px;

color: rgb(224, 108, 117); word-break:

break-all;">html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值