2

I want to recompile System.Web.Optimization to change the cache headers in Bundle.cs (CDN does not like the Vary header), since there appears to be no other way to override the headers. I am able to decompile the source (via Resharper), make the change, and recompile the source just fine but when I add the reference to my project all dependent Nuget packages give an error. Similar to the one below.

The type 'System.Web.Optimization.IBundleBuilder' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web.Optimization, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

I would rather not have to compile all dependencies. I am also open to other means of overriding the cache headers. HTTPModules, IIS etc.

skaffman
  • 398,947
  • 96
  • 818
  • 769
qrider
  • 41
  • 1
  • 5

1 Answers1

2

Instead of recompiling a custom version of bundles I just decided to route bundle requests through a different HttpHandler. A quick replace in the URL allows me to easily get the contents of a bundle and write it out with the cache headers I want. Not the most desirable method, but works.

Not allowing you to set your own headers in the library is a huge over sight. I hope they fix this soon.

    public void ProcessRequest(HttpContext context)
    {
        var request = context.Request;
        var response = context.Response;
        var cache = response.Cache;

        var path = request.Url.LocalPath;
        var bundlesPath = "~/" + path.Substring(path.IndexOf("mypath"));
        bundlesPath = bundlesPath.Replace("mypath", "bundle");


        Bundle bundle = BundleTable.Bundles.GetBundleFor(bundlesPath);
        var bundleContext = new BundleContext(new HttpContextWrapper(HttpContext.Current), BundleTable.Bundles, bundlesPath);
        var bundleResponse = bundle.GenerateBundleResponse(bundleContext);

        cache.SetCacheability(HttpCacheability.Public);
        cache.SetExpires(DateTime.UtcNow.AddYears(1));
        cache.SetMaxAge(new TimeSpan(365, 0, 0, 0));
        cache.SetValidUntilExpires(true);

        // This handler is called whenever a file ending 
        // in .sample is requested. A file with that extension
        // does not need to exist.
        response.ContentType = bundleResponse.ContentType;
        response.Write(bundleResponse.Content);
    }
Mikael Engver
  • 4,634
  • 4
  • 46
  • 53
qrider
  • 41
  • 1
  • 5