I just began working with VS 2012 RC. I’ve created a test site with a master page and a single web form. Currently, I’m using this code to bundle the entire Styles folder on the site:
Global.asax
BundleTable.Bundles.EnableDefaultBundles();
Site.master
<link rel="stylesheet" type="text/css" href="Styles/css" />
Question: The test site has a site-level CSS file that controls the overall look and feel of the site. In addition to the site-level CSS, each page could have their own CSS definitions. Is it possible to include only the site.css file in the master page, and then conditionally add .css files to the bundle as each page requires?
I tried this in the code behind of Default.aspx but it didn’t work:
BundleTable.Bundles.Add(new Bundle("~/Styles/Default.css"));
My suggestion:
Goto
Global.asax. Ensure the methodApplication_Startcontains following line:Find or create class
BundleConfigas follows, preferrably in folderApp_Start:Now use corresponding bundle in every appropriate page:
Or better from code:
(this is
cshtml, butaspxsyntax is for sure very similar).If you do not want to take care about adding each and every page manually to the method above. You could automate it. Following code could give you an idea how:
Usage: