20

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;
  }
}
  • Are there any potential issues with the fact that I am setting this static property on every web request? (The web app will run on a web farm)
  • Are there better ways to handle this?
Hao Kung
  • 28,040
  • 6
  • 84
  • 93
jessegavin
  • 74,067
  • 28
  • 136
  • 164
  • 2
    I don't see any fault, though I'd recommend using an ActionFilter and possibly storing the cached on/off in a cookie/session variable (until changed by recalling `DisableOptimizations=false`). – Brad Christie Jan 10 '13 at 20:12
  • @BradChristie I love the idea of storing it in session! – jessegavin Jan 10 '13 at 21:41

1 Answers1

9

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();
}
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • This is seriously awesome. Thanks a ton. – jessegavin Jan 10 '13 at 22:06
  • I was almost finished writing my own implementation based on your comment, but I like yours more! – jessegavin Jan 10 '13 at 22:07
  • 6
    It appears System.Web.Optimization.BundleTable.EnableOptimizations is a persistent global property, not per request. When it is set to true/false, it remains at that true/false value for all subsequent requests to the application from anyone. – Ben Amada Aug 25 '13 at 19:50
  • 8
    This is not thread safe! If it does actually work you can end up affecting other requests because BundleTable.EnableOptimizations sets a static field under the hood. – Brandon Cuff Mar 14 '14 at 16:25
  • A thread-safe alternative can be found here: http://stackoverflow.com/a/24444226/1232258 – extremeandy Nov 22 '16 at 00:01