ASP.NET MVC 3 Razor: Include JavaScript file in the head tag

You can use Named Sections.

_Layout.cshtml

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

_SomeView.cshtml

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

Leave a Comment