ค้นหาเว็บไซต์

ASP.NET MVC 3 Razor: รวมไฟล์ JavaScript ในแท็ก head


ฉันกำลังพยายามหาวิธีใช้งานไวยากรณ์ Razor ที่ถูกต้องเพื่อให้ไฟล์ JavaScript สำหรับ *.cshtml ใด ๆ อยู่ในแท็ก head พร้อมกับไฟล์รวมอื่น ๆ ทั้งหมดที่ถูกกำหนดใน _Layout.cshtml

คำตอบ #1

คุณสามารถใช้ ส่วนที่ตั้งชื่อแล้ว ได้

_Layout.cshtml

<head>
    <script type="text/javascript" src="@Url.Content("/Scripts/jquery-1.6.2.min.js")"></script>
    @RenderSection("JavaScript", required: false)
</head>

_SomeView.cshtml

@section JavaScript
{
   <script type="text/javascript" src="@Url.Content("/Scripts/SomeScript.js")"></script>
   <script type="text/javascript" src="@Url.Content("/Scripts/AnotherScript.js")"></script>
}

บทความที่เกี่ยวข้อง