Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
ASP.NET jQuery Cookbook (Second Edition)

You're reading from   ASP.NET jQuery Cookbook (Second Edition) Over 60 recipes for writing client script in ASP.NET 4.6 applications using jQuery

Arrow left icon
Product type Paperback
Published in Feb 2016
Publisher
ISBN-13 9781782173113
Length 478 pages
Edition 2nd Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Sonal Merchant Sonal Merchant
Author Profile Icon Sonal Merchant
Sonal Merchant
Sonal Aneel Allana Sonal Aneel Allana
Author Profile Icon Sonal Aneel Allana
Sonal Aneel Allana
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Getting Started with jQuery in ASP.NET FREE CHAPTER 2. Using jQuery Selectors with ASP.NET Controls 3. Event Handling Using jQuery 4. DOM Traversal and Manipulation in ASP.NET 5. Visual Effects in ASP.NET Sites 6. Working with Graphics in ASP.NET Sites 7. Ajax Using jQuery 8. Creating and Using jQuery Plugins Index

Bundling jQuery in ASP.NET MVC

Model View Controller (MVC) is a design pattern that separates design (Model), presentation (View), and action (Controller). Because of its popularity with developers, Visual Studio provides ready templates that are used to create MVC projects.

Similar to web forms, jQuery can be included in MVC views using the <script> tag. In this example, however, let's take a look at the use of bundling for this purpose.

Bundling helps you reduce the number of HTTP requests made by the browser. It is a feature that allows style sheets, JavaScript, or other files to be combined together in a single file called a bundle. This combined file can be downloaded as one unit using a single HTTP request.

Getting ready

  1. Launch a new ASP.NET Web Application project in Visual Studio using the Empty template. Ensure that the MVC checkbox is checked, as shown in the following screenshot:
    Getting ready
  2. This will create a project with MVC folders. Right-click on the Controllers folder in the Solution Explorer tab, and go to Add | Controller... as shown in the following screenshot:
    Getting ready
  3. This will launch the Add Scaffold dialog box. Select MVC 5 Controller – Empty, and click on the Add button:
    Getting ready
  4. On being prompted to add a name for the controller, type HomeController and click on the Add button:
    Getting ready
  5. Next, open the HomeController in the source mode, and right-click on the Index action method, as shown in the following screenshot. Click on Add View... as shown in the following screenshot:
    Getting ready
  6. This will launch the Add View dialog box. From the Template field, select Empty (without model). Uncheck the Use a layout page option and click the Add button to continue:
    Getting ready

    Note

    In the remaining recipes, when asked to create a MVC application, follow steps 1 to 6 as mentioned earlier.

  7. To use bundling, we need to install the ASP.NET Web Optimization package. This can be done from NuGet. From the File menu, launch NuGet by navigating to Project | Manage NuGet Packages. Select Microsoft.AspNet.Web.Optimization from the list of available packages. If the package is not visible, search for web.optimization, as shown in the following screenshot. Click on the Install button to download and install the latest version:
    Getting ready
  8. Lastly, create a Scripts folder in the project and include the jQuery library files in the folder.

How to do it…

Follow these steps to bundle jQuery in ASP.NET MVC:

  1. Open the BundleConfig class in the App_Start folder in the MVC project. If the file does not exist, create a new module (VB)/class (C#) in the App_Start folder, and name it BundleConfig.vb/BundleConfig.cs.
  2. In BundleConfig.vb/BundleConfig.cs, add a namespace to System.Web.Optimization at the top of the file:

    For VB, the code is as follows:

    Imports System.Web.Optimization

    For C#, the code is as follows:

    using System.Web.Optimization;
  3. Register and configure a bundle for jQuery in the RegisterBundles method in BundleConfig as follows:

    For VB, the code is as follows:

    Public Module BundleConfig
       Public Sub RegisterBundles(ByVal bundles As BundleCollection)
          bundles.Add(New ScriptBundle("~/Scripts/jquery").Include(
                    "~/Scripts/jquery-{version}.js"))
       End Sub
    End Module

    For C#, the code is as follows:

    public class BundleConfig
    {
        public static void RegisterBundles(BundleCollection bundles)
        {
           bundles.Add(new ScriptBundle("~/Scripts/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));
        }
    }
  4. To enable bundling in the development mode (optional), add the following code to the RegisterBundles method:

    For VB, the code is as follows:

    BundleTable.EnableOptimizations = True

    For C#, the code is as follows:

    BundleTable.EnableOptimizations = true;
  5. In the Global.asax file, include the namespace for System.Web.Optimization, as shown in step 2 mentioned previously. Then, register the bundle in the Application_Start method as follows:

    For VB, the code is as follows:

    BundleConfig.RegisterBundles(BundleTable.Bundles)

    For C#, the code is as follows:

    BundleConfig.RegisterBundles(BundleTable.Bundles);
  6. Now, open the Index view and include the namespace for System.Web.Optimization, as shown in the following code:

    For VB, the code is as follows:

    @Imports System.Web.Optimization

    For C#, the code is as follows:

    @using System.Web.Optimization
  7. Next, add the script reference for jQuery to the view in the <head> element as follows:
    @Scripts.Render("~/Scripts/jquery")

Note

Bundling is disabled in the debug mode by setting the debug attribute to true in the <compilation> element in the web.config file. To override this setting and enable bundling in the debug mode, set the EnableOptimizations property of the BundleTable class to true in the RegisterBundles method.

Unless EnableOptimizations is set to true, or the debug attribute is set to false, the files will not be bundled and the debug versions of the files will be used instead of the minified versions.

How it works…

Bundling jQuery in ASP.NET MVC can be done by following these steps:

  1. The wildcard string used for bundling jQuery ~/Scripts/jquery-{version}.js includes the development as well as the minified versions. The .vsdoc file, which is used by IntelliSense, is not included in the bundle.
  2. When the debug mode is on, the corresponding debug version is used. In the release mode, the minified version is bundled.
  3. On running the view in a browser, the bundled file can be seen on viewing the source in the browser window, as shown in the following HTML markup:
    How it works…

See also

The Using a CDN to load jQuery in MVC recipe

You have been reading a chapter from
ASP.NET jQuery Cookbook (Second Edition) - Second Edition
Published in: Feb 2016
Publisher:
ISBN-13: 9781782173113
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image