C# “@Scripts.Render”和“<script>”有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17701781/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:15:07  来源:igfitidea点击:

What is the difference between "@Scripts.Render" and "<script>"?

c#asp.netrazor

提问by Guilherme Oderdenge

I want to know about the difference between @Scripts.Render("~/something.js")and <script type="text/javascript" src="/something.js"></script>.

我想知道@Scripts.Render("~/something.js")和之间的区别<script type="text/javascript" src="/something.js"></script>

Yes, I've already searched about this subject, but with no success. I think if @Scripts.Renderexists isn't by chance.

是的,我已经搜索过这个主题,但没有成功。我认为如果@Scripts.Render存在不是偶然的。

More details

更多细节

What I'm meaning is: when I should use one or other and why.

我的意思是:什么时候我应该使用一个或另一个以及为什么。

采纳答案by Tamim Al Manaseer

Scripts.Render is used for bundling, if you bundle multiple scripts together and give them a name, then you can render them all together using this statement.

Scripts.Render 用于捆绑,如果您将多个脚本捆绑在一起并给它们一个名称,那么您可以使用此语句将它们一起渲染。

On debug mode, they'll render multiple tags, and in production you can deploy a single bundled script. Debug mode is set true or false in the web.config:

在调试模式下,它们将呈现多个标签,而在生产中,您可以部署单个捆绑脚本。调试模式在 web.config 中设置为 true 或 false:

  <system.web>
    <compilation debug="true" ... />

Hereis more about bundling.

这里有更多关于捆绑的信息。

回答by Erik Schierboom

Functionally, they both have the same result in your example. The Scripts.Renderis part of the System.Web.Optimizationnamespace, which is included in the Microsoft.AspNet.Web.Optimization NuGet package. As such, Scripts.Renderalso support JavaScript bundles (see http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification).

在功能上,它们在您的示例中都具有相同的结果。的Scripts.Render是金System.Web.Optimization名字空间,它包括在Microsoft.AspNet.Web.Optimization NuGet包。因此,Scripts.Render还支持 JavaScript 包(参见http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification)。