Bundling not work for me -MVC - asp.net-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;

Related

aspmvc - Script bundle on debug how to force reload

I have a ASP.NET MVC5 application and I have the following bundle:
bundles.Add(new ScriptBundle("~/bundles/appcomponents").Include(
"~/Scripts/js/jquery.min.js",
"~/Scripts/js/bootstrap.min.js",
"~/Scripts/js/modernizr.min.js",
"~/Scripts/js/detect.js",
"~/Scripts/js/fastclick.js",
"~/Scripts/js/jquery.slimscroll.js",
"~/Scripts/js/jquery.blockUI.js",
"~/Scripts/js/waves.js",
"~/Scripts/js/wow.min.js",
"~/Scripts/js/jquery.nicescroll.js",
"~/Scripts/js/jquery.scrollTo.min.js"));
When I'm in DEBUG mode I would like the browser to reload the scripts, actually I don't know if this already happens. If I set:
BundleTable.EnableOptimizations = true;
Then it works because I only get one dynamic reference, but in Developement (DEGUB) I don't get the dynamic parameter:
I was trying to do something like this but It didnt work:
Any clue? Or by default when you have bundle it doesn't matter if you are in DEBUG mode you always get the latest script?
By my understanding, you want to always get the newest scripts when in development (so no bundling and minification)
when you say debug, make sure your web.config should have compilation to debug="true", like below:
<system.web>
<compilation debug="true" />
<!-- Lines removed for clarity. -->
</system.web>
then bundling and minification should be turn off
if you set BundleTable.EnableOptimizations = true;, that means even when <compilation debug="true" />, bundling and minification will still function.
so you problem probably is because you are setting BundleTable.EnableOptimizations = true; which means you have activated bundling and minification all the time even in debug hence may be not getting the newest script
if bundling and minification is turn off but you still not seeing the newest script, it maybe browser is caching it, try load it in chrome incognito mode
more info about controlling bundling and minification, checkout official doc: https://learn.microsoft.com/en-us/aspnet/mvc/overview/performance/bundling-and-minification#controlling-bundling-and-minification

MVC ScriptBundle CDN version regular and min versions of AngularJS

From what I have read, I know that MVC bundles can "smartly" include the correct js file (minified or regular) based on whether you are debugging or not. Ideally, I'd like it to load the standard AngularJS when I'm debugging, and the minified version when I am not.
I have the following in my BundleConfig.cs:
bundles.UseCdn = true;
bundles.Add(new ScriptBundle("~/bundles/angular", "https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.js")
.Include(~/Scripts/angular.js"));
and in my view:
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
#Scripts.Render("~/bundles/angular")
}
It doesn't seem to work unless I add BundleTable.EnableOptimizations = true; to BundleConfig.cs, but I am skeptical of this. Is this OK to leave in for development AND production? Is this forcing it to always use the regular or minified version in all cases, as opposed to smartly switching like I want? From what I can tell, it appears to be using the non-minified version regardless of whether I am running with the debugger or not, or in release configuration or not.
The other option seemed to be setting debug=false in the web.config, but this prevents me from debugging without manually modifying the file, and I'd like it to be handled automatically if possible.
Then I noticed that MVC uses a * syntax for jquery, which happens to have normal and minified versions included:
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.unobtrusive*",
"~/Scripts/jquery.validate*"));
So I tried .Include(~/Scripts/angular*")); thinking it was a wildcard that would include .js or .min.js as appropriate but that didn't seem to work either.
Should I be doing something differently?

Loading an MVC JS bundle externally

I have a small JS library in my MVC 5 project that I want to be available for external users to load into their apps. At the moment I'm bundling it like so:
bundles.Add(new ScriptBundle("~/clientApi")
.IncludeDirectory("~/Api/clientapps/", "*.js"));
I can then access the bundled library via browser at the path /clientApi.
However, it's always minified, even though I've set my web.config debug=true, and other bundles in my own app are included as non-minified.
How can I make the file/s in the bundle available as a non-minified bundle file?
If you access /clientApi directly then yes it will be the bundled/minified version.
The debug=true option effects your script reference in your own .cshtml file. When debug=true, references to the individual script files are rendered to the client (so the client doesn't use /clientApi at all).
When debug=false, then a reference to /clientApi (with the version query string) is rendered to the client instead, so they get the bundled/minified version... If you give that link to these external users, then that is what is going to get rendered.
That path doesn't care if it is debug or not. It's not like /clientApi is going to bundle but not minify the files depending on your compilation settings... it's just either your app is going to render the bundled/minified path or the individual script paths.
If you want to do debugging/testing in external apps, then they will just have to use the individual script paths.
Even if you do give these external apps the /clientApi reference once testing is done and they are ready to use the bundled/minified version, it doesn't explain how you are going to handle versioning. If you update a script, how will they know to stop caching?
Actually you can serve the bundle unminified if you disable the transforms of the Bundles
protected void Application_Start() {
BundleTable.EnableOptimizations = true; // Force bundling to occur
// If the compilation node in web.config indicates debugging mode is enabled
// then clear all transforms. I.e. disable Js and CSS minification.
if (HttpContext.Current.IsDebuggingEnabled) {
BundleTable.Bundles.ToList().ForEach(b => b.Transforms.Clear());
}
}

ASP.NET MVC 4 is bundling js files in Debug Mode

"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.

Basic Bundling, Minification in ASP.NET

ASP.NET MVC 4.0 application - Visual Studio 2012
I cannot get bundling and minification to work in release mode.
My basic non-understanding is:
Do I have to provide the *.MIN.css, *.MIN.js files beforehand, or should VS minify the files on its own? (ie: I provide a mcimagemanager.js and VS makes a mcimagemanager.MIN.js out of it) ???
Here is a code snippet - which gets called in Global.asax:
public static void RegisterBundles(BundleCollection bundles)
{
var im = new ScriptBundle("~/bundles/MCImageManager").Include(
"~/Scripts/tinymce/plugins/imagemanager/js/mcimagemanager.js"
);
bundles.Add(im);
}
it works fine in Debug - not in Release-mode
Thank you!
No you don't need to provide .min file, nor will the bundler create that version (not something you see in the folder, at least).
The difference is this. Let's say you have both jquery-1.9.1.js and jquery-1.9.1.min.js in your scripts folder.
Debug mode will use jquery-1.9.1.js as the source script, and no it won't be minified or bundled, as the whole bundling/minification is disabled in debug mode (though you can override this).
Release mode will use jquery-1.9.1.min.js AND bundle it with other scripts for that bundle.
If you only have the one file, jquery-1.9.1.js, Release mode will use it and minify and bundle it.
Debug mode will NOT use .min files. So if you use a wildcard to include all files for a scripts directory, your .min files will not be included.
Debug mode, if you look at the rendered HTML, will reference all script files in the bundle individually. In Release mode, there will only be one script reference (with a querystring for versioning) per bundle.
Other relevant reading/posts:
Scripts.Render using outdated javascript file
Force ASP.Net MVC Bundle to render the javascript files in a certain order
Bundling and minification framework do also minification itself. So you just provide plain JavaScript/CSS files. It handle on the one hand files itself in the other hand it handle the registration in the view, for eample: #Scripts.Render("~/bundles/jquery"). So it "know" what file needs to be included in the view, weather it is "normal" or "min" version.
More on that topic you can find in this nice Exercise: http://msdn.microsoft.com/en-us/vs11trainingcourse_aspnetandvisualstudio_topic5.aspx
However this is might even better resource for the MVC oriented application: http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification

Resources