2

我想重新编译 System.Web.Optimization 以更改 Bundle.cs 中的缓存标头(CDN 不喜欢 Vary 标头),因为似乎没有其他方法可以覆盖标头。我能够反编译源代码(通过 Resharper),进行更改,然后重新编译源代码就好了,但是当我将引用添加到我的项目时,所有依赖的 Nuget 包都会出错。与下面的类似。

'System.Web.Optimization.IBundleBuilder' 类型在未引用的程序集中定义。您必须添加对程序集 'System.Web.Optimization, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' 的引用

我宁愿不必编译所有依赖项。我也对覆盖缓存头的其他方法持开放态度。HTTPModules、IIS 等

4

1 回答 1

2

我没有重新编译自定义版本的包,而是决定通过不同的 HttpHandler 路由包请求。URL 中的快速替换使我可以轻松获取捆绑包的内容并使用我想要的缓存标头将其写出。不是最理想的方法,但有效。

不允许您在库中设置自己的标题是一个巨大的过失。我希望他们能尽快解决这个问题。

    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);
    }
于 2014-08-08T05:29:53.880 回答