我没有重新编译自定义版本的包,而是决定通过不同的 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);
}