답변:
만약
(1) 이와 같은 _Layout.cshtml 뷰가 있습니다.
<html>
<body>
@RenderBody()
</body>
<script type="text/javascript" src="~/lib/layout.js"></script>
@RenderSection("scripts", required: false)
</html>
(2) Contacts.cshtml이 있습니다
@section Scripts{
<script type="text/javascript" src="~/lib/contacts.js"></script>
}
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h2> Contacts</h2>
</div>
</div>
(3) About.cshtml이 있습니다
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h2> Contacts</h2>
</div>
</div>
레이아웃 페이지에서 필요한 경우 false "@RenderSection ("scripts ", required : false)"로 설정되면 페이지가 렌더링되고 사용자가 페이지에 관한 경우 contacts.js가 렌더링되지 않습니다.
<html>
<body><div>About<div>
</body>
<script type="text/javascript" src="~/lib/layout.js"></script>
</html>
required가 true "@RenderSection ("scripts ", required : true)"로 설정된 경우, 페이지가 렌더링되고 사용자가 ABOUT 페이지에있을 때 contacts.js STILL이 렌더링됩니다.
<html>
<body><div>About<div>
</body>
<script type="text/javascript" src="~/lib/layout.js"></script>
<script type="text/javascript" src="~/lib/contacts.js"></script>
</html>
짧은 페이지에서, true로 설정되면 다른 페이지에서 필요 여부에 관계없이 어쨌든 렌더링됩니다. false로 설정 하면 하위 페이지가 렌더링 될 때만 렌더링됩니다.
렌더 섹션의 정의는 MSDN
레이아웃 페이지에서 명명 된 섹션의 내용을 렌더링합니다. MSDN
_layout.cs 페이지에서
@RenderSection("Bottom",false)
여기에서 bootom 섹션의 내용을 렌더링하고 false
부울 속성을 지정하여 섹션이 필요한지 여부를 지정합니다.
@section Bottom{
This message form bottom.
}
즉, 모든 페이지에서 섹션을 맨 아래로 나타내려면 Rendersection 메서드에서 두 번째 매개 변수로 false를 사용해야합니다.
GetAllEmployees.cshtml이 있다고 가정합니다
<h2>GetAllEmployees</h2>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
// do something ...
</thead>
<tbody>
// do something ...
</tbody>
</table>
//Added my custom scripts in the scripts sections
@section Scripts
{
<script src="~/js/customScripts.js"></script>
}
스크립트가없는 또 다른보기 "GetEmployeeDetails.cshtml"
<h2>GetEmployeeByDetails</h2>
@Model.PageTitle
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
// do something ...
</thead>
<tbody>
// do something ...
</tbody>
</table>
내 레이아웃 페이지 "_layout.cshtml"
@RenderSection("Scripts", required: true)
따라서 GetEmployeeDetails.cshtml로 이동하면 GetEmployeeDetails.cshtml에 렌더링 할 섹션 스크립트가 없다는 오류가 발생합니다. 나는에 플래그를 변경하는 경우 @RenderSection()
에서 required : true
에``필요 : FALSE '를. 이는 뷰의 @ 섹션 스크립트에 정의 된 스크립트를 렌더링하는 것을 의미합니다. 세련된 접근 방식은 _layout.cshtml에 있습니다.
@if (IsSectionDefined("Scripts"))
{
@RenderSection("Scripts", required: true)
}
Section not defined: "scripts".
필요한 플래그를 설정할 때 정보 페이지를 렌더링 할 때 시간이 표시됩니다true
.