29.05.2015 Views

o_19mgorv9t13a3ko71fev19l81mqa.pdf

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 26<br />

Bundles<br />

In this chapter, I am going to look at the bundles feature, which the MVC Framework provides to organize and optimize the CSS<br />

and JavaScript files that views and layouts cause the browser to request from the server. Table 26-1 provides the summary for<br />

this chapter.<br />

Table 26-1. Chapter Summary<br />

Problem Solution Listing<br />

Define bundles<br />

Create instances of the<br />

table.<br />

StyleBundle and ScriptBundle classes and add them to the bundle<br />

1–6<br />

Ensure that the Views/web.config file includes a reference for the<br />

Prepare an application for bundles<br />

7<br />

System.Web.Optimization namespace.<br />

Add a bundle to a view or layout Use the Styles.Render and Scripts.Render helpers. 8<br />

Enable concatenation and minification of<br />

bundles<br />

Set the debug attribute of the compilation attribute in the Web.config file to false. 9<br />

Preparing the Example Application<br />

For this chapter, I have created a new MVC project called ClientFeatures using the Empty template option, checking<br />

the option to add the core MVC folders and references.<br />

Adding the NuGet Packages<br />

The bundles feature that I describe in this chapter makes it easier to manage JavaScript and CSS files. To that end, I am going to<br />

install a number of NuGet packages that are commonly used for client-side development. Select Package Manager<br />

Console from the Visual Studio Tools Library Package Manager menu and enter the following<br />

commands:<br />

Install-Package jQuery –version 1.10.2<br />

Install-Package jQuery.Validation –version 1.11.1<br />

Install-Package Microsoft.jQuery.Unobtrusive.Validation –version 3.0.0<br />

Install-Package Bootstrap -version 3.0.0<br />

Install-Package Microsoft.jQuery.Unobtrusive.Ajax –version 3.0.0<br />

Creating the Model and Controller<br />

I am going to create a variation on the application that I used in the previous chapter, so I started by creating a new class file<br />

called Appointment.cs in the Models folder. You can see the contents of this file in Listing 26-1.<br />

Listing 26-1. The Contents of the Appointment.cs File<br />

using System;<br />

using System.ComponentModel.DataAnnotations;<br />

691

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!