ASP.NET MVC 3 Razor:在 head 标记中包含 JavaScript 文件

我正在尝试找出正确的 Razor 语法,以获取特定 *.cshtml 的 JavaScript 文件以及 _Layout.cshtml 中定义的所有其他包含文件.

I'm trying to figure out the proper Razor syntax to get a JavaScript file for a particular *.cshtml to be in the head tag along with all the other include files that are defined in _Layout.cshtml.

推荐答案

你可以使用命名部分.

You can use Named Sections.

_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>
}

相关文章