为什么使用@Scripts.Render("~/bundles/jquery") [英] Why use @Scripts.Render("~/bundles/jquery")
问题描述
怎么样
@Scripts.Render("~/bundles/jquery")
与像这样从 html 中引用脚本不同
differ from just referencing the script from html like this
<script src="~/bundles/jquery.js" type="text/javascript"></script>
是否有任何性能提升?
推荐答案
捆绑就是将几个没有任何格式的 JavaScript 或样式表文件(也称为缩小)压缩到一个文件中,以保存带宽和加载请求的数量页面.
Bundling is all about compressing several JavaScript or stylesheets files without any formatting (also referred as minified) into a single file for saving bandwith and number of requests to load a page.
例如,您可以创建自己的捆绑包:
As example you could create your own bundle:
bundles.Add(New ScriptBundle("~/bundles/mybundle").Include(
"~/Resources/Core/Javascripts/jquery-1.7.1.min.js",
"~/Resources/Core/Javascripts/jquery-ui-1.8.16.min.js",
"~/Resources/Core/Javascripts/jquery.validate.min.js",
"~/Resources/Core/Javascripts/jquery.validate.unobtrusive.min.js",
"~/Resources/Core/Javascripts/jquery.unobtrusive-ajax.min.js",
"~/Resources/Core/Javascripts/jquery-ui-timepicker-addon.js"))
然后渲染成这样:
@Scripts.Render("~/bundles/mybundle")
@Scripts.Render("~/bundles/mybundle")
相对于原生 <script src="~/bundles/mybundle"/>
的另一个优势code> 是 @Scripts.Render()
将尊重 web.config
调试设置:
One more advantage of @Scripts.Render("~/bundles/mybundle")
over the native <script src="~/bundles/mybundle" />
is that @Scripts.Render()
will respect the web.config
debug setting:
<system.web>
<compilation debug="true|false" />
如果 debug="true"
那么它将为每个源脚本呈现单独的脚本标签,而不进行任何缩小.
If debug="true"
then it will instead render individual script tags for each source script, without any minification.
对于样式表,您必须使用 StyleBundle 和 @Styles.Render().
For stylesheets you will have to use a StyleBundle and @Styles.Render().
不是使用单个请求(使用脚本或链接标签)加载每个脚本或样式,而是将所有文件压缩成一个 JavaScript 或样式表文件并一起加载.
Instead of loading each script or style with a single request (with script or link tags), all files are compressed into a single JavaScript or stylesheet file and loaded together.
这篇关于为什么使用@Scripts.Render("~/bundles/jquery")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!