Skip to main content

.NET 4.0 Bundle of Joy

It use to be that you would have to summon the mighty powers of YUI compressor and roll out the red carpets of MSBuild to minify CSS and JS files in your ASP.NET and MVC applications.  With the introduction of bundling in System.Web.Optimization (now Microsoft.AspNet.Web.Optimization) you can skip the formalities and bundle your JS and CSS file without introducing additional dependencies.

To use bundling in a .NET 4.0 app.
1- Create an App_Start folder in your solution (if you don't have one already)
2- Add a class in your App_Start folder for your bundles and create a function to call all your CSS and JS files.

E.g. (BundleConfig.cs)

Code Snippet
  1. using System.Web;
  2. using System.Web.Optimization;
  3.  
  4. namespace YourGlobalNameSpace
  5. {
  6.     public class BundleConfig
  7.     {
  8.         public static void RegisterBundles(BundleCollection bundles)
  9.         {
  10.             bundles.Add(new StyleBundle("~/styles/style").Include
  11.                 ("~/style/style1.css"));
  12.             bundles.Add(new ScriptBundle("~/scripts/script").Include
  13.                 (("~/scripts/script1.js"));
  14.         }
  15.     }
  16. }

3- Add reference to the optimization classes and register your bundles on Application_Start() event in Global.asax.

First
Code Snippet
  1. using System.Web.Optimization;

Then
Code Snippet
  1. protected void Application_Start()
  2. {
  3.     YourGlobalNameSpace.RegisterBundles(BundleTable.Bundles);
  4. }

4- Call your bundles on any page with Styles.Render or Scripts.Render function on your .aspx or .cshtml (for razor) pages.

E.g.

@Styles.Render("~/styles/style") // for CSS
@Scripts.Render("~/scripts/script") // for JS

After adding the render function to your page you will have all your JS and CSS files on your page every time your page loads. To see your little .NET bundle of joy full with minified JS and CSS just change your compilation settings from debug = "true" to "false" in web.config.

Comments

Popular posts from this blog

Processing ASP MVC Web API Requests in Multi-threaded JS Web Worker

Unlike an asynchronous Ajax call, HTML5 Web workers provide an opportunity to run a Multi-threaded JavaScript code in modern browsers that support them . Each worker spawns an isolated thread with dedicated JavaScript Event Loop, Stack and Heap memory. For example a regular Ajax Call to MVC Web API service call goes through the following asynchronous call cycle. The JavaScript Event Loop in this case could be interrupted by events that are being executed on the UI; for instance, a "window.alert", could possibly stop all scripts on the page from executing until a user responds. Replacing the Ajax Call with HTML5 web worker provides a great way to run long running scripts in separate threads so that asynchronous code execution is not interrupted by UI events. Here is the a JavaScript worker implementation of the same MVC Web API call using a JavaScript web worker. Despite the advantages of using a web worker, implementing one requires working with some constr...

Turning WCF Service into .asmx for debugging

Even though .asmx web services are becoming dinosaurs of the new .NET world of WCF. I missed the simplicity of debugging code right in visual studio without: Creating a client to consume WCF service Attaching w3p.exe process and Adding break points  One quick solution: Turn WCF service into .asmx service with few lines of code, debug your code with asmx, and turn .asmx off during deployment.  Detail steps: 1- First take your WCF class and add WebService attribute to it Code Snippet /// <summary> /// Dual mode with .ASMX and WCF /// </summary> [ WebService (Namespace = "http://www.yourdomain.com/" )] 2- Then add WebMethod attribute to a function you want to expose in .asmx Code Snippet [ WebMethod ] public List < PageController . Page > DualFunction() { 3- Take the .svc file from your solution - copy and rename the copied file [YourOriginalWCFFile.asmx]. Open up the copied file and rename "ServiceHost...

Optimizing ASP.NET/MVC 3.0 Site

I stumbled upon the Google Page Speed Insights tools  when testing ASP.MVC 3 site performance few days ago. After running the performance test, I found out that the web page I was testing has a performance index of 50% - i.e.things such as loading JS, CSS, and static files are taking away about half of the website loading time.  Here are some steps I took to optimize the site and recover much needed speed. Plan of Action 1- Enabling Gzip Compression Gzip  is a compression algorithm that is used by several web servers and browsers to send and receive compressed http responses. By default gzip is disabled on IIS (bummer ...). You could use IIS GUI to change the settings - but for those of you who are XML ninja's here are the few lines of code that will recover some speed for your .NET 4.0 /3.5 app running on IIS7. Code Snippet < system.webServer >    < urlCompression doStaticCompression = " true " doDynamicCompression = " true " /> ...