首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重新编译System.Web.Optimization以更改缓存头

重新编译System.Web.Optimization以更改缓存头
EN

Stack Overflow用户
提问于 2014-07-22 07:48:49
回答 1查看 265关注 0票数 2

我希望重新编译System.Web.Optimization以更改Bundle.cs中的缓存头(CDN不像like头),因为似乎没有其他方法来覆盖这些头。我能够解压缩源代码(通过Resharper),进行更改,并重新编译源代码,但是当我将引用添加到我的项目时,所有依赖的Nuget包都会出现错误。与下面的类似。

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

我宁愿不必编译所有的依赖项。我还可以使用其他方法覆盖缓存头。HTTPModules、IIS等。

EN

回答 1

Stack Overflow用户

发布于 2014-08-08 05:29:53

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

不允许您在库中设置您自己的标题,这是一个很大的疏忽。希望他们能尽快解决这个问题。

代码语言:javascript
复制
    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);
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24881630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档