Translator's note. ASP.NET Bundling and Minification is also known by other names: Microsoft ASP.NET Web Optimization Framework, System.Web.Optimization, Microsoft.Web.Optimization and ASP.NET Optimization - Bundling.
BundleConfig
class, which is located in the App_Start
directory. The following code shows the final version of the class: using System.Web.Optimization; public class BundleConfig { public static void RegisterBundles(BundleCollection bundles) { 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*")); bundles.Add(new ScriptBundle("~/bundles/WebFormsJs").Include( "~/Scripts/WebForms/WebForms.js", "~/Scripts/WebForms/WebUIValidation.js", "~/Scripts/WebForms/MenuStandards.js", "~/Scripts/WebForms/Focus.js", "~/Scripts/WebForms/GridView.js", "~/Scripts/WebForms/DetailsView.js", "~/Scripts/WebForms/TreeView.js", "~/Scripts/WebForms/WebParts.js")); bundles.Add(new ScriptBundle("~/bundles/MsAjaxJs").Include( "~/Scripts/WebForms/MsAjax/MicrosoftAjax.js", "~/Scripts/WebForms/MsAjax/MicrosoftAjaxApplicationServices.js", "~/Scripts/WebForms/MsAjax/MicrosoftAjaxTimer.js", "~/Scripts/WebForms/MsAjax/MicrosoftAjaxWebForms.js")); bundles.Add(new ScriptBundle("~/bundles/modernizr").Include( "~/Scripts/modernizr-*")); } }
Application_Start
method of the Global.asax
file: void Application_Start(object sender, EventArgs e) { BundleConfig.RegisterBundles(BundleTable.Bundles); AuthConfig.RegisterOpenAuth(); }
<asp:PlaceHolder />
control, as shown in the following code: <asp:PlaceHolder runat="server"> <%: Scripts.Render("~/bundles/modernizr") %> <%: Scripts.Render("~/bundles/jquery") %> <%: Scripts.Render("~/bundles/jqueryui") %> </asp:PlaceHolder>
ScriptManager
comment out the links to the jQuery scripts, as shown below: <body> <form runat="server"> <asp:ScriptManager runat="server"> <Scripts> <%-- <asp:ScriptReference Name="jquery" /> <asp:ScriptReference Name="jquery.ui.combined" /> --%> </Scripts> </asp:ScriptManager> <header>
Bundle.config
file, which contains the code for creating CSS bundles (style bundles). <?xml version="1.0" encoding="utf-8" ?> <bundles version="1.0"> <styleBundle path="~/Content/css"> <include path="~/Content/Site.css" /> </styleBundle> <styleBundle path="~/Content/themes/base/css"> <include path="~/Content/themes/base/jquery.ui.core.css" /> <include path="~/Content/themes/base/jquery.ui.resizable.css" /> <include path="~/Content/themes/base/jquery.ui.selectable.css" /> <include path="~/Content/themes/base/jquery.ui.accordion.css" /> <include path="~/Content/themes/base/jquery.ui.autocomplete.css" /> <include path="~/Content/themes/base/jquery.ui.button.css" /> <include path="~/Content/themes/base/jquery.ui.dialog.css" /> <include path="~/Content/themes/base/jquery.ui.slider.css" /> <include path="~/Content/themes/base/jquery.ui.tabs.css" /> <include path="~/Content/themes/base/jquery.ui.datepicker.css" /> <include path="~/Content/themes/base/jquery.ui.progressbar.css" /> <include path="~/Content/themes/base/jquery.ui.theme.css" /> </styleBundle> </bundles>
Bundle.config
file you can add your own style Bundles.Translator's note. In my opinion, the creation of style Bundles using theBundle.config
file restricts the developer, since the ability to add custom transformations of Bundles (classes that implement theIBundleTransform
interface) isIBundleTransform
. Now there are whole libraries of such transformations (for example, Bundle Transformer ) that allow to translate LESS-, Sass-, SCSS- and CoffeeScript-code, as well as use other code minimization algorithms (by default B / M uses some modification of Microsoft Ajax Minifier ) . Therefore, it is better to create style bundles in theBundleConfig
class (as in ASP.NET MVC). In order for the settings specified in theBundleConfig
class to work correctly you need to comment out the contents of thebundles
element in theBundle.config
file.
<%: Styles.Render("~/Content/themes/base/css", "~/Content/css") %> <%: Scripts.Render("~/bundles/modernizr") %> <%: Scripts.Render("~/bundles/jquery", "~/bundles/jqueryui") %>
Render
method.Translator's note. The author did not mention anything about the<webopt:BundleReference />
control, with which you can also add links to style Bundles in the markup of an ASP.NET page. Since this control does not support adding references to script Bundles and makes confusion, I recommend finding all the places where it is used and replacing it with calls to theStyles.Render
method.
<webopt:BundleReference />
control and the Bundle.config
file are Bundle.config
. Since the Bundle`
created in the BundleConfig
class become available only in the run mode, the styles specified in them are not applied in the Visual Studio designer window ( Design and Split display modes). This problem is exactly solved by the above tools. It seems that now you have to duplicate styles in the BundleConfig
class and in the BundleConfig
file, and comment out the contents of the bundles
element in the Bundle.config
file before deployment. Unfortunately, it is not yet clear what should be done when using LESS, Sass or SCSS in a project (most likely, you will have to use translation (compilation) of the code during project building or do without a designer).Source: https://habr.com/ru/post/150863/
All Articles