asp.net-mvc 如何使用 ASP.NET MVC Razor 在 head 部分渲染脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16611413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to render script in the head section using ASP.NET MVC Razor
提问by StringBuilder
I need to render some script in the head section of an ASP.NET MVC view. How can this be achieved?
我需要在 ASP.NET MVC 视图的 head 部分呈现一些脚本。如何做到这一点?
In ASP.NET we had ContentPlaceHolders in Master. What is the MVC equivalent of implementing this?
在 ASP.NET 中,我们在 Master 中有 ContentPlaceHolders。什么是实现这个的 MVC 等价物?
回答by user2031802
You can use @sectionlike this:
你可以这样使用@section:
Master file:
主文件:
@RenderSection("masterjs", required: false)
View file:
查看文件:
@section masterjs
{
<script type="text/javascript" src="@Url.Content("/Scripts/SomeScript.js")"></script>
}

