为什么要使用@ 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" />
的另一个优点是@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屋!