I am new to MVC so thought I would start a new project and try out some of the new features in MVC4. I have two css files in my Content
directory, normalise_mini
and site.css
. When I use the following code:
<link href="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/Content/css")" rel="stylesheet" type="text/css" />
It only takes my site.css
file not my normalisation file. I have the following in my app start:
protected void Application_Start()
{
// Remove all other view engines except razor:
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RazorViewEngine());
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
BundleTable.Bundles.RegisterTemplateBundles();
BundleTable.Bundles.EnableDefaultBundles();
}
Do I need to create a bundle for each css file (as this person is doing)? Or should it just find all the css files automatically (which I would expect to be the default behaviour). It might be worth noting that I started this project as a Empty Website based on Razor view engine (which actually wasn't empty at all :/)
Thanks in advance
Update
According to the link posted, I need to comment out the line that registers the template bundles. Eg:
protected void Application_Start()
{
// Remove all other view engines except razor:
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RazorViewEngine());
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
//BundleTable.Bundles.RegisterTemplateBundles(); // This is not needed
BundleTable.Bundles.EnableDefaultBundles();
}
This now works. My thoughts are that the two lines should co-exist without any problems. I guess it's by design, but is it bad design?
Someone else has already answered this question:
http://forums.asp.net/t/1776599.aspx/1?MVC+4+0+Bundling+Minification+not+working+
UPDATE:
Someone has expressed concern that a link may not be enough to answer the question. Although I still believe that someone who visits this page should read the linked thread, the short answer is to remove the BundleTable.Bundles.RegisterTemplateBundles() line from Application_Start() and replace it with BundleTable.Bundles.EnableDefaultBundles()
Note: EnableDefaultBundles was removed as of the 1.0.0 RTM version of Optmization, you can still get the equivalent functionality via setting up your own DynamicFolderBundles. But in general this is likely to cause issues (usually there are depedencies in files that are not captured when you bulk include *.js), so we moved to explicit bundle setup and away from EnableDefaultBundles.
For the best up to date docs/tutorials: Codeplex Documentation