在MVC4中捆绑的正确方法

bro*_*oke 10 asp.net-mvc asp.net-mvc-4

我对捆绑脚本和样式文件的正确方法感到困惑.目前,我的BundleConfig.cs看起来像这样:

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
            "~/Scripts/jquery-{version}.js"));

bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
            "~/Scripts/jquery-ui-{version}.js"));

bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
            "~/Scripts/jquery.unobtrusive*",
            "~/Scripts/jquery.validate*"));

// Use the development version of Modernizr to develop with and learn from. Then, when you're
// ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
            "~/Scripts/modernizr-*"));

bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));

bundles.Add(new ScriptBundle("~/bundles/knockout").Include(
            "~/Scripts/knockout-{version}.js",
            "~/Scripts/knockout-{version}.debug.js",
            "~/Scripts/knockout-sortable.js"));

bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
            "~/Content/themes/base/jquery.ui.core.css",
            "~/Content/themes/base/jquery.ui.resizable.css",
            "~/Content/themes/base/jquery.ui.selectable.css",
            "~/Content/themes/base/jquery.ui.accordion.css",
            "~/Content/themes/base/jquery.ui.autocomplete.css",
            "~/Content/themes/base/jquery.ui.button.css",
            "~/Content/themes/base/jquery.ui.dialog.css",
            "~/Content/themes/base/jquery.ui.slider.css",
            "~/Content/themes/base/jquery.ui.tabs.css",
            "~/Content/themes/base/jquery.ui.datepicker.css",
            "~/Content/themes/base/jquery.ui.progressbar.css",
            "~/Content/themes/base/jquery.ui.theme.css"));

bundles.Add(new StyleBundle("~/bundles/BootStrapcss").Include(
            "~/BootStrap/css/bootstrap.css",
            "~/BootStrap/css/bootstrap-fileupload.css"));

bundles.Add(new StyleBundle("~/bundles/BootStrap").Include(
            "~/BootStrap/tpg-main.css",
            "~/BootStrap/tpg-internal.css"));

bundles.Add(new ScriptBundle("~/bundles/BootStrapjs").Include(
            "~/BootStrap/js/bootstrap-fileupload.js",
            "~/BootStrap/js/bootstrap.js"));

BundleTable.EnableOptimizations = true;
Run Code Online (Sandbox Code Playgroud)

应该留在我拥有的,或将我的所有脚本文件捆绑成一个ScriptBundle,并将我的所有样式合并为一个StyleBundle?我希望尽可能达到最佳性能.

asy*_*ult 14

如果你总是使用所有文件而不是将其粘贴在两个包中; 一个用于javascript,一个用于样式.较少的捆绑包意味着对服务器获取资源的请求较少,这可能导致第一次访问时性能略有提高; 随后,浏览器将缓存文件.

如果你并不总是使用所有文件,那么将它们分成更多的包就更有意义了.

  • 为了澄清他的观点,您可能只需要在一页或两页上使用Knockout,而不是整个站点.因此,您不应将其与其他所有内容捆绑在一起,因为它将被加载到未使用或不需要的页面上. (6认同)