My ASP.NET MVC4 project works fine in debug builds, as long as I don't mind repeatedly rebuilding and republishing when it gets a little confused and "loses" the bundles, as it often does.
It appears the "losing" of bundles is related somehow to one of my layouts, which inserts the content of the CSS bundles directly into the page, to generate templates for our doc management to display "documents" which consist of form fields. If I build and then the first thing I do is load the page with that layout first, MVC4 decides the bundles don't exist, until I do a clean build, and reload the page with a different layout.
So I'm doing something out of the ordinary there.
Now I've tried a release build, and the try-again method doesn't work with that. MVC generates the following tags in the page. These resources do not exist on the server. There is no bundles
directory. The Content
and Content/themes/base
directories exist, but nothing in them is called css
, and I get a 404 when I try to load any of the linked resources. The page has no CSS and no script.
This code in my layout file...
@Styles.Render("~/Content/css")
@Styles.Render("~/Content/themes/base/css")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryui")
...becomes...
<link href="/MyApp/Content/css?v=BoqupuQxZQpXG1tSIMNZQqNVpJ_E-NUxg_QIsyvmKn01" rel="stylesheet"/>
<link href="/MyApp/Content/themes/base/css?v=myqT7npwmF2ABsuSaHqt8SCvK8UFWpRv7T4M8r3kiK01" rel="stylesheet"/>
<script src="/MyApp/bundles/modernizr?v=qVODBytEBVVePTNtSFXgRX0NCEjh9U_Oj8ePaSiRcGg1"></script>
<script src="/MyApp/bundles/jquery?v=JzhfglzUfmVF2qo-weTo-kvXJ9AJvIRBLmu11PgpbVY1"></script>
<script src="/MyApp/bundles/jqueryui?v=EHft7nwilnDMnejvwTyEKn_C5lwIsN1M3Xyb8c9OmSQ1"></script>
How do you make this work? Is it a configuration thing? I found one guy here whose solution is to say that "it should work", and I agree. However, in my case it actually turns out not to work, so that solution may not be the right one for my particular situation here.
I've noticed a fair number of things in MVC4 are simply broken, and the bundling feature is undeniably buggy; is the Release configuration just not functional at all?
I've tried the suggestion to turn off debug in web.config
,
<compilation debug="false" targetFramework="4.5" />
...but that has no effect. Anyway I can't find a way to force Web.Release.config
to insert that into the output web.config
.
UPDATE
If I put
BundleTable.EnableOptimizations = true;
...at the bottom of BundleConfig.RegisterBundles
, it does the bundling thing correctly in the VS dev server when I "run" by hitting F5. If I change that to false
and hit F5, it renders a set of script and whatnot tags referencing the individual files in the bundles. Both work correctly -- on my dev machine. So let's set it to false
and publish...
BundleTable.EnableOptimizations = false;
That works when I publish to the server, even if it's a Release build.
So, there's a workaround.