I need to be able to temporarily disable bundling and minification for a single request for the purpose of debugging JavaScript & CSS Issues. I would like to do this at run time by adding a parameter to the QueryString like so..
http://domain.com/page?DisableOptimizations=true
Here's the approach I am considering.
protected void Application_BeginRequest(object sender, EventArgs e)
{
// Enable for every request
BundleTable.EnableOptimizations = true;
// Disable for requests that explicitly request it
bool disable;
bool.TryParse(Context.Request.QueryString["DisableOptimizations"], out disable);
if (disable)
{
BundleTable.EnableOptimizations = false;
}
}
Bundling and minification is enabled or disabled by setting the value of the debug attribute in the compilation Element in the Web. config file. In the following XML, debug is set to true so bundling and minification is disabled. To enable bundling and minification, set the debug value to "false".
Bundling and minification are two techniques you can use in ASP.NET to improve page load performance for your web application. Bundling combines multiple files into a single file. Minification performs a variety of different code optimizations to scripts and CSS, which results in smaller payloads.
What is bundling and minification. Bundling and minification are two distinct performance optimizations you can apply in a web app. Used together, bundling and minification improve performance by reducing the number of server requests and reducing the size of the requested static assets.
To optimize the performance of an application I found that bundling and minification can significantly improve the performance. It can be applied on MVC as well as in ASP.NET web forms.
Extending what I mentioned in a comment:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class OptimizationsDebuggingAttribute : ActionFilterAttribute
{
private const String PARAM_NAME = "DisableOptimizations";
private const String COOKIE_NAME = "MySite.DisableOptimizations";
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
Boolean parsedPref;
Boolean optimizationsDisabled = false;
if (filterContext.HttpContext.Request.QueryString[PARAM_NAME] != null)
{ // incoming change request
var pref = filterContext.HttpContext.Request.QueryString[PARAM_NAME].ToString();
if (Boolean.TryParse(pref, out parsedPref))
{
optimizationsDisabled = parsedPref;
}
}
else
{ // use existing settings
var cookie = filterContext.HttpContext.Request.Cookies[COOKIE_NAME];
if (cookie != null && Boolean.TryParse(cookie.Value, out parsedPref))
{
optimizationsDisabled = parsedPref;
}
}
// make the change
System.Web.Optimization.BundleTable.EnableOptimizations = !optimizationsDisabled;
// save for future requests
HttpCookie savedPref = new HttpCookie(COOKIE_NAME, optimizationsDisabled.ToString())
{
Expires = DateTime.Now.AddDays(1)
};
filterContext.HttpContext.Response.SetCookie(savedPref);
base.OnActionExecuting(filterContext);
}
}
then of course implementing it looks something like:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new OptimizationsDebuggingAttribute());
}
Or, if you prefer the hands-on approach:
[OptimizationsDebugging]
public ActionResult TroublesomeAction()
{
return View();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With