ASP.NET MVC中的布局

Kar*_*sla 1 asp.net-mvc razor asp.net-mvc-4 asp.net-mvc-layout

我已经开始学习Asp.Net MVC了,问题是当我添加一个名为Index.cshtml它的新视图时,会自动从中获取Html Layout Page.我不知道这里发生了什么.

Index.cshtml: -

@{
   ViewBag.Title = "Index";
 }

 <h2>Index</h2>
Run Code Online (Sandbox Code Playgroud)

布局页面: -

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
</head>
<body>
<p>This is a Layout Page....</p>
@RenderBody()

@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Kar*_*sla 6

只需包含Layout = null,您的问题将解决为: -

Index.cshtml: -

@{
   ViewBag.Title = "Index";
   Layout = null;
 }

 <h2>Index</h2>
Run Code Online (Sandbox Code Playgroud)

问题是由于'Layout'属性的使用,如果您没有明确指定值,则将使用该_ViewStart.cshtml文件进行布局.Layout=null在视图中指定将使其忽略_ViewStart.cshtml文件中指定的布局.

编辑: -

如果您想了解更多信息_ViewStart.cshtml及其工作原理,请访问此链接: -

_ViewStart.cshtml布局文件在何处以及如何链接?

要么

http://www.dotnetcurry.com/showarticle.aspx?ID=605