Is it possible to use bundling and minification from Microsoft.AspNet.Web.Optimization without having an MVC project?
I'm creating an AngularJS site communicating with a REST API. For the REST API I'm using ASP.NET Web API. I have also created an "ASP.NET Empty Web Application". There are only HTML, js and CSS files in this project (and a web.config). I'd like for my js and CSS files to be bundled and minified, but I don't want to create a MVC project just to get that. Is it possible?
To enable bundling and minification, set the debug value to "false". You can override the Web. config setting with the EnableOptimizations property on the BundleTable class. The following code enables bundling and minification and overrides any setting in the Web.
Both bundling and minification are the two separate techniques to reduce the load time. The bundling reduces the number of requests to the Server, while the minification reduces the size of the requested assets.
To optimize the performance of an application I found that bundling and minification can significantly improve the performance. It can be applied on MVC as well as in ASP.NET web forms.
What is bundling and minification. Bundling and minification are two distinct performance optimizations you can apply in a web app. Used together, bundling and minification improve performance by reducing the number of server requests and reducing the size of the requested static assets.
It is absolutely possible to use the bundling and minification in a blank project.
Install-Package Microsoft.AspNet.Web.Optimization
Create a BundleConfig Class and define your bundles:
using System.Web.Optimization; public class BundleConfig { public static void RegisterBundles(BundleCollection bundles) { bundles.Add(new ScriptBundle("~/bundles/js").Include( "~/Scripts/*.js")); bundles.Add(new StyleBundle("~/bundles/css").Include( "~/Styles/*.css")); } }
Register the BundleConfig class within the application start in the global.asax
void Application_Start(object sender, EventArgs e) { BundleConfig.RegisterBundles(BundleTable.Bundles); }
In addition to the answers given above, I'd like to mention that there was one important step forgotten:
After you've installed the NuGet package for Microsoft.AspNet.Web.Optimization
and registered the bundles in the Global.asax (as explained in Claies answer), you need to add the render methods for styles and scripts as follows:
<%= Styles.Render("~/bundles/css") %> <%= Scripts.Render("~/bundles/MattsUIBundle/js") %>
This needs to be added to the ASPX page's head section in order to render the bundles "~/bundles/js" and "~/bundles/css" added earlier to your page. Without that it will not appear (see why do I need render?). It requires that you add
<%@ Import Namespace="System.Web.Optimization" %>
to line 2 of your page in order to register the namespace, assuming you have already added the NUGET package Microsoft.AspNet.Web.Optimization
to your code.
If you want to include more related files, do it like
void Application_Start() { BundleCollection bundles=BundleTable.Bundles; var jsMattsUIBundle = new ScriptBundle("~/bundles/MattsUIBundle/js"); jsMattsBundle.Include("~/Scripts/lib/jquery.min.js"); jsMattsBundle.Include("~/Scripts/lib/jquery-ui.custom.min.js"); jsMattsBundle.Include("~/Scripts/lib/jquery.watermark.min.js"); bundles.Add(jsMattsBundle); }
or more simply
jsMattsBundle.Include("~/Scripts/lib/jquery.min.js", "~/Scripts/lib/jquery-ui.custom.min.js", "~/Scripts/lib/jquery.watermark.min.js");
That will bundle together all the 3 scripts under the virtual path "~/bundles/MattsUIBundle/js"
.
Important: Bundling will check if you are in debug mode or release mode. Optimizations only apply if you remove the debug flag in web.config
<compilation debug="true" />
or if you explicitly define inside of Application_Start
you want to optimize regardless of being in debug mode:
BundleTable.EnableOptimizations = true;
Likewise, if you're using CDN support, turn it on via
BundleTable.Bundles.UseCdn = true; //enable CDN support
which will allow you to declare
var jqueryCdnPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js"; BundleTable.Bundles.Add(new ScriptBundle("~/bundles/jquery", jqueryCdnPath).Include("~/Scripts/jquery-{version}.js"));
Note that the CDN will only be used in release mode. The following script ensures that a local copy of jQuery is loaded if the CDN loading fails:
<%= Scripts.Render("~/bundles/jquery") %> <script type="text/javascript"> if (typeof jQuery == 'undefined') { var e = document.createElement('script'); e.src = '@Url.Content("~/Scripts/jquery-1.7.1.js")'; e.type = 'text/javascript'; document.getElementsByTagName("head")[0].appendChild(e); } </script>
assuming that you're providing a local copy of jQuery 1.7.1 at "~/Scripts"
.
Note: In MVC Razor syntax rendering is done in the view as follows:
@Scripts.Render("~/bundles/MattsUIBundle/js") @Styles.Render("~/bundles/css")
More information can be found here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With