I don’t know if I’m doing something wrong but it’s probably a bug inside MVC4. I wonder how can I fix this?
Working scenario
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
ScriptBundle scriptBundle = new ScriptBundle("~/js");
string[] scriptArray =
{
"~/content/plugins/jquery/jquery-1.8.2.min.js",
"~/content/plugins/jquery/jquery-ui-1.9.0.min.js",
"~/content/plugins/jquery/jquery.validate.min.js",
"~/content/plugins/jquery/jquery.validate.unobtrusive.min.js",
"~/content/plugins/bootstrap/js/bootstrap.min.js",
};
scriptBundle.Include(scriptArray);
scriptBundle.IncludeDirectory("~/content/js", "*.js");
bundles.Add(scriptBundle);
BundleTable.EnableOptimizations = true;
}
}
@Scripts.Render("~/js")
Converts to (e.g. IT WORKS!)
<script src="/js?v=VeCPNK561DZp34yjmWbLrNM35Kf6gaNDl0xsMMC25BQ1"></script>
Not so much working scenario
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
ScriptBundle scriptBundle = new ScriptBundle("~/js");
string[] scriptArray =
{
"~/content/plugins/jquery/jquery-1.8.2.min.js",
"~/content/plugins/jquery/jquery-ui-1.9.0.min.js",
"~/content/plugins/jquery/jquery.validate.min.js",
"~/content/plugins/jquery/jquery.validate.unobtrusive.min.js",
"~/content/plugins/bootstrap/js/bootstrap.min.js",
};
scriptBundle.Include(scriptArray);
scriptBundle.IncludeDirectory("~/content/js", "*.js");
bundles.Add(scriptBundle);
// BundleTable.EnableOptimizations = true; // I could set it to 'false' for same result, it's false by default
}
}
@Scripts.Render("~/js")
Converts to (e.g. IT DOES NOT WORK!)
(nothing, couple of empty break lines)
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
If my understanding is correct, you should define your bundles using the “non-min” JavaScript files. When you enable optimizations it will swap the non-min files for the min files for you:
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
ScriptBundle scriptBundle = new ScriptBundle("~/js");
string[] scriptArray =
{
"~/content/plugins/jquery/jquery-1.8.2.js",
"~/content/plugins/jquery/jquery-ui-1.9.0.js",
"~/content/plugins/jquery/jquery.validate.js",
"~/content/plugins/jquery/jquery.validate.unobtrusive.js",
"~/content/plugins/bootstrap/js/bootstrap.js",
};
scriptBundle.Include(scriptArray);
scriptBundle.IncludeDirectory("~/content/js", "*.js");
bundles.Add(scriptBundle);
}
}
Optimizations are set to false when debugging, but are true by default in release mode.
Method 2
yes, It doesn’t work properly with files like *.min.js.
“if you add a file to your bundle that has a name that ends in .min.js
(like I did) and optimizations aren’t enabled (e.g. debug is set to
true in web.config and BundleTable.EnableOptimizations has not been
set to true), this file will be ignored (i.e no script include will be
generated for it in your html).”
Here you can read full original response:
http://blog.degree.no/2013/06/javascript-file-missing-resource-bundle-asp-net-web-optimization-framework/
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0