ASP.NET MVC 4 is bundling js files in Debug Mode - asp.net-mvc

"The issue is that, by default, asp.net mvc DOES NOT bundle the css and js files in debug mode. But our css and js files ARE getting bundled in the debug mode."
For some reason, all the css and js files are being bundled in debug mode. It started few days ago. We have a large team with several developers and its hard to find out what changed in the last few days because there are several changes submitted to the repository. However I didn't find any significant changes in the BundleConfig and Global.asax.cs.
When the application is running in debug mode, BundleTable.EnableOptimizations returns a false.
To my understanding when debug is set to true then bundling does not happen.
<compilation debug="true" targetFramework="4.5">
So far I haven't found a single occurrence of this issue on Google. Is this a very unique problem?
Ideally instead of a workaround I would like to fix it. Finding the cause is the actual problem here. Any pointers on where I should start looking for the fix would be appreciated. Thank you.
Edit: My question is similar to but not exactly the same as ASP.NET MVC 4 app with bundling and minification, why is minification enabled in debug mode?
Someone please remove the "This question may already have an answer here:" tag.
In my case the bundle paths are already starting with a "~"
Template:
#Styles.Render("~/Content/css")
#Scripts.Render("~/bundles/js")
Bundle Config:
bundles.Add(new Bundle("~/Content/css").Include("~/Content/*.css"));
bundles.Add(new Bundle("~/bundles/js").Include("~/Scripts/myproj.*"));

Try adding this in your global Application_Start()
BundleTable.EnableOptimizations = false;
So...
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
BundleTable.EnableOptimizations = false;
}

For me the issue was in the web.config "debug" was set to false.
<configuration>
<system.web>
<compilation debug="false" targetFramework="4.0" />

Try troubleshooting outside of MVC specifically.
Does this problem now transcend your entire team or just your machine? If it's the entire team at least you know the change happened somewhere in the commit cycle.
Have you rebuilt the solution? Cleaned properly? Are you running IIS, IIS Express, or Cassini? Are you absolutely certain that the site/build you're running in the debugger is not in release mode? You may be debugging a specific build that isn't being modified through the build pipeline (failed builds definitely fall under this category).
I'm using VS 2010 and MVC 4 and by default in debug mode, I see absolutely no bundling. I don't have to explicitly set anything. If you're seeing differently, something obviously has to account for it as this isn't normal behavior.
I have a feeling your best approach is the hardest: unwind your commits. Make a copy of your working directory and revert to sometime before this happened. Verify the problem does not occur in this build, don't just assume. Now checkout a commit half way between that point in time and now. Test again. If the build is still good, cut the time in half and repeat. Once you've hit the issue, you know the commit it was introduced in is somewhere between your last "good" commit and the one you're currently testing. This technique greatly simplifies isolating a problem to a specific commit. I almost never go one commit at a time unless I'm completely unsure of the code or I already have a good indication of where it might be.

In my case I had used
#Scripts.Render("/Bundles/scriptname")
When I replaced it with
#Scripts.Render("~/Bundles/scriptname")
it worked. The '~' made all the difference.

Related

Relative paths in CSS not valid when using MVC w/Bundles

I've been developing a MVC5 web application for several months. I've published to each of 3 servers used for development, testing and the intended public server. Everything has been tested by a team of a dozen beta testers and a decision was made to go live with the web app this weekend.
Prior to publishing the web app to the live (public) host I modified the web.config to disable debug mode for the public site. After publishing, all kinds of problems cropped up related to missing CSS and JS resources.
After reading a lot of articles regarding Bundles and 404 errors, I found one that hinted to add the following to Web.config:
<modules runAllManagedModulesForAllRequests="true">
<remove name="BundleModule" />
<add name="BundleModule" type="System.Web.Optimization.BundleModule" />
</modules>
This resolved the 404 issues for the StyleBundle and ScriptBundle configurations, but now I have 404 errors for images that previously worked fine. I'm not sure of the best way to resolve these. I don't want to relocate the images and I don't want to edit the CSS since these are distribution files (jQueryUI, ThemeRoller, DataTables, etc). I want to leave their distribution folder structure and original source files (CSS and JS) unmodified.
An example of the problem.
DataTables distribution is in my ~/Scripts folder:
/Scripts/DataTables-1.10.2/
/Scripts/DataTables-1.10.2/media/css
/Scripts/DataTables-1.10.2/media/images
/Scripts/DataTables-1.10.2/media/js
Bundles configuration:
bundles.Add(new ScriptBundle("~/bundles/DataTables").Include(
"~/Scripts/DataTables-1.10.2/media/js/jquery.dataTables.js"));
bundles.Add(new StyleBundle("~/bundles/DataTables.css").Include(
"~/Scripts/DataTables-1.10.2/media/css/jquery.dataTables.css"));
jquery.dataTables.css contains references to ../images/someimage.png and with Web.config debug mode enabled this works flawlessly. Now that debug mode has been disabled and Bundles are minifying/combining, I am getting 404 errors:
http://example.com/GenericError.htm?aspxerrorpath=/images/someimage.png"
It seems as though the image URL is now assumed to be relative to /Bundles/ - though I'm not positive.
There must be an additional configuration I'm missing. Can someone point me in the right direction?
EDIT
Raphael's comments on this question and his URL to another similar SO question did not help to resolve this problem. Sean's recommendation of BundleTransformer seems like it might work but I don't find any documentation on how to install this package.
See my answer at:
CSS/JS bundle in single file in mvc when publish with release option
It deals with this exact issue and the options you have to resolve it.

Bundling not work for me -MVC

RegisterBundles :
bundles.Add(new ScriptBundle("~/bundles/AllScripts").Include(
"~/Scripts/jquery.x123.{version}.js",
"~/Scripts/bootstrap.js",
"~/Scripts/jqRect.js"));
In the Shared Layout file :
#Scripts.Render("~/bundles/AllScripts")
In Global.asax we have :
BundleConfig.RegisterBundles(BundleTable.Bundles);
The scripts didn't combined, also didn't get minified.
This is in release mode.
Is there anything missed?
In your web.config you need to set the attribute debug="false" in the <compilation>-tag.
This means that you can use this flag to allow javascript debugging locally before deploying (debugging minified and bundled javascript is obviously next to impossible).
NOTE: The "Release mode" flag only affects the way the C# (or VB.NET) compiler compiles your classes and is not related to the debug attribute of the <compilation>-tag. Also note that the debug attribute controls whether ASP.NET MVC caches the location of views on disk and thus has a great performance impact: you should always have debug=false in a production environment.
When you develop your project with Debug mode, it doesn't combined and minified. However,
you can force it to do that by setting
BundleTable.EnableOptimizations = true;

Precompiling Views using MvcBuildViews

I want to ship pre-compiled views. I've seen a few questions regarding specifying
Changing the following in the csproj
<MvcBuildViews>true</MvcBuildViews>.
and
and modifying the web.config (since i am using entityframework)
<compilation debug="true" targetFramework="4.0">
<buildProviders>
<remove extension=".edmx"/>
</buildProviders>
</compilation>
it now takes longer to compile which i was expecting however im not sure if it is working correctly
can i presume i am right in saying that if configured correctly it should actually build the views into the binary rather than just doing a precompilation validation?
if true then should i prevent the webdeployment from shipping the cshtmls? will the presence of them onsite cause a recompile?
if so this should result in a faster start time for deployed mvc applications? as they don't need to compile the views?
should I add false to the csproj as well? i saw an article which suggested i should change it from true to false, but it isn't present in my csproj.
Cheers
Tim
can i presume i am right in saying that if configured correctly it should actually build the views into the binary rather than just doing
a precompilation validation?
No.
if true then should i prevent the webdeployment from shipping the
cshtmls? will the presence of them onsite cause a recompile?
No, cshtml files must be present when you ship.
if so this should result in a faster start time for deployed mvc
applications? as they don't need to compile the views?
No, in Release mode there's an optimization made by Razor which automatically will cache the location of views and once it parses them it doesn't reparse on subsequent requests.
So, the moral of the story is that you could enable the MvcBuildViews option in Release mode so that your build server could detect any potential errors during the continuous integration build of the project. In Debug mode I keep it disabled because it slows down building.
RazorGenerator seemed to be the only option to my original question which was to ship compiled views as binaries.

MVC Routes/RouteDebugger work when deployed to IIS7 but not in Cassini

I am currently working on an MVC3 Application, and after fighting with some routes, noticed some odd behaviors:
I have added new routes, but these are not reflecting when I run the application with F5 in Cassini via Visual Studio 2010. If I deploy this same code to a server (with the same web.config) running IIS7, my new routes work.
I have installed Phil Haack's RouteDebugger tool. The web.config is set to have this enabled, but it does not display when running my site via Visual Studio 2010/Cassini. It works properly when deployed to a remote server with IIS7 - again, same config file.
Any thoughts? Is there some config/setup option that I am missing?
Thanks!
Looked to be a weird caching issue. After fighting with this for a day, by chance I opened up a different branch (main) of the same application, which worked fine.
Performed a reverse/forward integration and the main branch worked fine, dev branch still would not show the changes.
Did a comparison, and they were exactly the same bits.
Did a 'Get Specific Version' from TFS, overwriting everything, and the problem persisted.
Finally, I deleted both branches from my local drive, and then did a Get Specific Version, and suddenly everything worked. Odds are this solved the issue because it forced all of the bin/obj files to be wiped out (although in theory they should have been rebuilt since I was doing a Rebuild Solution).
All in all a really weird issue, so I figured I'd post this just in case someone runs into the same issue down the line, given the difficulty of debugging this.

What is the difference in ASP.NET startup between a recycle and a recompile?

When my MVC app is first deployed it works fine, up until the point when the application pool is recycled. After that, none of the routes work and I receive 404s for everything. Forcing a rebuild by editing the web.config or redeploying will bring the site back online, until the next recycle.
My MVC setup is a bit atypical. I'm doing quite a lot on startup, such as scanning assemblies for routes that need to be loaded. I feel like there must be some Global.asax event that only fires on the initial build but is not run on a recycle.
What are the differences between the recycle and the rebuild in terms of startup events?
Thanks,
Brian
The answer is apparently nothing. My problems stemmed from a garbage collection issue where resources weren't being cleaned up.

Resources