Scripts.Render using outdated javascript file - c#

My MVC4 application is using Scripts.Render to load a bundle that is loading a file called "functions.js."
When I debug this application in the browser, the script loads, but the version is outdated. When I view the resource directly, but append ?v=anytext the script looks correct, but without that appended, the script shows the old code. Is there a way to force the bundling to output the correct file instead of a stale one?

This is likely a caching issue. When using Bundling and Minification in debug mode (when <compilation debug="true" /> is set in your web.config), bundling/minification is disabled.
You can override this and force bundling and minification by adding BundleTable.EnableOptimizations = true;. This will make it act like it will in your production environment, where everything is bundled and minified, and the script reference includes that versioning parameter (like you gave) that will force the browser to reload your scripts when anything changes.
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
//all your bundle code
BundleTable.EnableOptimizations = true;
}
}

Related

Changes not taking effect when class is removed from theme.css - MVC5 ASP.NET

I have an application, that currently has a Theme.css I need to remove a class on the theme, I have removed the class, but when I have merged my branch into the default branch the changes are not taking effect.
I have checked in the BundleConfig in App_Start this is what I have:
bundles.Add(new StyleBundle("~/Content/theme").Include(
"~/skin/default_skin/css/theme.css",
"~/admin-tools/admin-forms/css/admin-forms.css",
"~/plugins/select2/css/core.css"));
Now in Content/theme the theme.css folder is not included in the project. But I have made my changes in the file: /skin/default_skin/css/theme.css but then when I merge my branch into the live site the change is undone.
But ~/Content/theme.css is excluded from the project.
Turns out that it was due to the way MVC is bundled in.Net also factoring in the way the minification is happening. It was bundling the theme, but any changes to it where excluded from the project build, so I think when I was changing the files it was ignoring the changes, building it then minifying the files.

Scripts.Render is not working

I'm developing ASP.NET MVC Razor project using monodevelop 5.9.6 and I added all packages necessary for bundling (System.Web.Optimization), in BundleConfig I'm adding
bundles.add(ScriptBundle("~/bundles/jquery").Include("~/Scripts/jquery-{version}.js"));
and others. In Global.asax.cs I'm calling
BundleConfig.RegisterBundles(BundleTable.bundles);
However in _Layout.cshtml
#Scripts.Render("~/bundles/jquery")
is rendering as
<script src="/bundle/jquery?v=5GM9HLcujnDGm6SNVq0Es63_cXK2viQ4_nYEpm02Ls1"></script>
when running, causing "Failed to load resource (404)" javascript error, because all jquery's files are not rendered as it should be.
I need to render all jquery files and style files.
It's a feature of Bundling and Minification. All of jquery and css file grouped into one file when you enable Bundling and Minification and it works in two conditions
protected void Application_Start()
{
//Enabling Bundling and Minification
BundleTable.EnableOptimizations = true;
}
<compilation debug="false" targetFramework="4.0">
IF you want each file render separately use
compilation debug = "true" and
BundleTable.EnableOptimizations = false;.
But it's not a good practice when deploying code to production server.
Why 404 : If you can share your rendering code for jquery and css file I can try why 404 error is coming.enter code here
Thanks

MVC Bundles - force minification even if a .min file is present

Consider the following scenario:
I use Web Essentials, thus my .less files get compiled into .css, .map en -min.css files. I also use MVC StyleBundles.
Expectation:
In release builds, the StyleBundle "handler" bundles and minifies all the .css files defined in the bundle. (not the .min files)
Actual result:
In release builds, the StyleBundle "handler" does bundle, but uses the .min files generated by Web Essentials, if found. Else wise is minifies the .css itself.
Can I configure the "handler" to ignore the generated .min files and always minify the .css files itself?
Deleting all present .map en -min.css files and generating a solution-wide Web Essentials setting in which I disable the generation of minified files and is not a feasible solution in my team.
Thanks!
A quick and easy solution to this would be the following:
Web Essentials allows you to create a solution-wide settings file that can be checked into your version control system.
The option to create this file is under the Web Essentials menu that gets added when it gets installed.
Note: Some plugins may conflict with this and I've had one PC where the Web Essentials menu was missing even after several attempts to re-install.
After you click this, it will create a file WebEssentials-Settings.json in your solution directory where you can customize settings that will get used by all of your team members when they load up the solution the next time.
To disable the automatic minification LESS files, just change the CompileOnBuild, CompileOnSave, and MinifyInPlace settings to false.

Do you need to build after editing javascript files?

I have an ASP.NET project that I run via IIS locally.
When I change my .cs files I have to build to see the changes when I access my website via browser.
Do I have to build when I change my .js files?
Please justify your answer.
No you don't have to build your project after a change in a javascript file because javascript files don't require compilation.
Just pressing ctrl+F5 (to clear your browsers cache) in the browser is sufficient to view your changes.

ASP .NET MVC 4.5 Style Bundling not outputting anything

I have an ASP .NET MVC 4 app with BundleConfig.cs in the App_Start folder and a call to this class and the RegisterBundles method within the Global.asax.
Everything works fine regarding the Script bundling, but the style bundling produces nothing.
var bundle = new StyleBundle("~/bundles/css")
.Include("~/Themes/Rikkle.Web/Styles/app.min.css");
BundleTable.Bundles.Add(bundle);
I access the above bundle on the page like so:
<link href="/bundles/css" rel="stylesheet" type="text/css"/>
I am manually calling the link as opposed to using #Styles.Render() because the page output NOTHING when I call this (again, all script bundling still works). When I go to localhost:xxx/bundles/css in the browser I get a Status Code of 200, everything is fine, just that the payload is nothing. When I go to localhost:/themes/rikkle.web/styles/app.min.css in the browser, it pulls up without an issue.
Also, I am referencing System.Web.Optimization, the latest drop from Nuget, in both my views folder web.config and the main web.config.
This question is answered by: Bundler not including .min files
Essentially, if you have "min," as in "app.min.css," then the bundling blows up.

Categories