![]() Inside that method, add a call to your static class/method, passing in MVC’s BundleTable.Bundles object so the custom bundles can be added: protected void Application_Start()īundleConfig.RegisterBundles(BundleTable.Bundles) Inside this file is, typically, an Application_Start method. By default, you’re given a file containing your application’s programmatic configuration. Register the bundle configurationĬongratulations! The hard-well, tedious-part is done! Next, we just need to link our bundle configuration with the MVC application. Once you’re done with each bundle, add it to the collection passed in as a parameter (in this example, we’ve called it bundles): bundles.Add(myBundle) Ĭontinue this pattern until you’ve configured all of the bundles you’d like. Thus, “ ~/js/app.js“ might become “ localhost:1234/js/app.js“ in development and “ in production, no questions asked. ![]() Using this bundle, you can simply add new files (or entire directories!) by using its methods: myBundle.IncludeDirectory("~/js/vendor/") Ī nice feature of these methods is that you can specify your paths as “app-relative”-that is, relative to the root of your application, whether it be in a subfolder or not-with the “tilde” notation. var myBundle = new ScriptBundle("~/bundles/js") You’ll need to specify a unique path for each bundle, which we’ll use to output references to our MVC views later. Both of these classes provide built-in minification, thus reducing your total payload even further. MVC provides two tailored classes for our purposes: ScriptBundle, for JavaScript files, and StyleBundle, for CSS. Next, inside this method, create one or more Bundle instances. Public static void RegisterBundles(BundleCollection bundles) To start, create a new, static class called BundleConfig (or “ BundleConfiguration“, if you’re feeling verbose) with a single static method called RegisterBundles: public static class BundleConfig We’ll take this such approach for our bundles, as well. The default project templates then provide static methods of modifying these collections with app-specific details. Common collections include RouteTable.Routes and GlobalFilters.Filters, but MVC adds a new one: BundleTable.Bundles. If you’ve worked with MVC in the past, you’re no doubt familiar with the concept of configuration collections, as seen in a site’s file. Let’s go over these steps in more detail. Add a reference to your bundle to your MVC views.Register your bundle configuration with your Web application.Create a bundle configuration to instruct MVC how to combine and compress (minify) your files. ![]() ![]() To get started with bundling in MVC4, you’ll need to perform a few straightforward steps: The server component of the application, written in ASP.NET MVC 4, provided us with an excellent way of providing bundled, minified scripts with its built-in bundling feature. Though the application we developed was quite complex (and featured about two dozen external script files), it was targeted at iPad users, among others, and therefore needed to load quickly, even over a slow cellular network. ![]() In a project we recently completed at Headspring, a coworker and I encountered this problem quite early. Many also perform a process known as “minification”, whereby non-essential whitespace is removed and variable names are changed to be as short as possible, resulting in an unreadable-but highly efficient-piece of code. To combat this problem, developers often use tools called “bundlers”, which concatenate script files-or stylesheets-together into a single file, thereby reducing the I/O overhead of sending multiple files across the Internet. Start with jQuery and some application libraries like Backbone.js (and its dependency, Underscore), then throw in plugins from UI frameworks like Foundation and some form validation scripts-not to mention your app’s code itself!-and you can easily discover your app has dozens of external files, each of which must be loaded on every page refresh. A Quick-Start of ASP.NET MVC 4's BundlingĪs a Web application becomes more complex, the number of external scripts on which it depends grows significantly. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |