Search code examples
c#asp.netasp.net-mvcvisual-studio-2012bundling-and-minification

Bundling and minification without ASP.NET MVC


Is it possible to use bundling and minification from Microsoft.AspNet.Web.Optimization without having an MVC project?

I'm creating an AngularJS site communicating with a REST API. For the REST API I'm using ASP.NET Web API. I have also created an "ASP.NET Empty Web Application". There are only HTML, js and CSS files in this project (and a web.config). I'd like for my js and CSS files to be bundled and minified, but I don't want to create a MVC project just to get that. Is it possible?


Solution

  • It is absolutely possible to use the bundling and minification in a blank project.

    1. Use Nuget to install the package: Install-Package Microsoft.AspNet.Web.Optimization
    2. Create a BundleConfig Class and define your bundles:

      using System.Web.Optimization;
      public class BundleConfig
      {
          public static void RegisterBundles(BundleCollection bundles)
          {
              bundles.Add(new ScriptBundle("~/bundles/js").Include(
                        "~/Scripts/*.js"));
              bundles.Add(new StyleBundle("~/bundles/css").Include(
                         "~/Styles/*.css")); 
          } 
      }
      
    3. Register the BundleConfig class within the application start in the global.asax

      void Application_Start(object sender, EventArgs e)
      {
          BundleConfig.RegisterBundles(BundleTable.Bundles);
      }
      
    4. reference the bundles in your HTML document.
    5. Enable bundling by disabling debug mode.