Why use @Scripts.Render(“~/bundles/jquery”)

匿名 (未验证) 提交于 2019-12-03 02:20:02

问题:

How does

@Scripts.Render("~/bundles/jquery") 

differ from just referencing the script from html like this

Are there any performance gains?

回答1:

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")) 

And render it like this:

@Scripts.Render("~/bundles/mybundle") 

For stylesheets you will have to use a StyleBundle and @Styles.Render().

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.



回答2:

You can also use:

@Scripts.RenderFormat("", "~/bundles/mybundle") 

To specify the format of your output in a scenario where you need to use Charset, Type, etc.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!