Programming asp

Page 307

Figure 13-13. YSlow’s Components tab shows how many components this page requests

Let’s see how we can use ASP.NET MVC 4’s built-in bundling feature to reduce the number of HTTP requests on this page.

Putting ASP.NET MVC to Work ASP.NET MVC 4 and .NET Framework 4.5 provide a new System.Web.Optimization library that offers bundling and minification support out of the box. It provides basic features for bundling different resources according to custom rules (we’ll see how in coming sections), as well as a built-in JavaScript minifier and stylesheets. It also includes automatic cache busting—i.e., invalidating the browser cache when any content changes. Another important feature that it offers is the ability to duplicate stylesheets and script files automatically (as long as they are not in different paths). For most scenarios, this will suffice. However, you can always choose to implement third-party solutions if your application’s needs demand more.

Bundling and Minification If you inspect the head of Layout.cshtml, you’ll notice two new helpers provided by the System.Web.Optimization library, @Styles and @Scripts: @Styles.Render("~/Content/themes/base/css", "~/Content/css") @Scripts.Render("~/bundles/modernizr")

As their names suggest, they bundle and minify stylesheets and JavaScript files or resources, respectively. Putting ASP.NET MVC to Work | 289

www.it-ebooks.info


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.