asp.net-mvc - MVC 5 捆绑和 Azure CDN(查询字符串)

标签 asp.net-mvc azure caching model-view-controller bundling-and-minification

我一直在关注这个教程:https://azure.microsoft.com/en-us/documentation/articles/cdn-serve-content-from-cdn-in-your-web-application/

一切都很好,直到我注意到捆绑的脚本和 CSS 文件返回时带有 cache: no-cacheexpires: -1pragma: no -缓存 header 。 当然,这与Azure无关。为了证明这一点,我通过直接从我的网站而不是 CDN(即 CDN)访问它们来测试这些 bundle 。 mysite.com/bundles/mybundle?v={myassembleversion}。结果是一样的。当我禁用 CDN 并使用 MVC 生成的 v 查询字符串访问捆绑文件时, header 符合预期:公共(public)缓存,有效期为一年。

我尝试实现 IBundleTransform 接口(interface),但是 context.BundleVirtualPath 是只读的(即使它说获取或设置虚拟路径... )。我还尝试修改 Application_EndRequest() 处的响应 header ,但它也不起作用。我的最后一个赌注是编写 IIS 出站规则,但由于我的 bundle (与“自定义”v 查询字符串一起使用)不返回 Last-Modified header ,所以这也是一次徒劳的尝试。

我的问题是:如果我希望将捆绑的文件缓存在客户端上(即,直到 v 查询字符串发生更改),如何将 MVC 捆绑与 Azure CDN 结合使用?

最佳答案

我知道我有点晚了,但我确实找到了解决方法。我正在使用 Frison B Alexander here 的代码。

问题是,一旦重写 StyleBundles 或 ScriptBundles 的查询字符串,一年的默认缓存行为将重置为无缓存。这是通过为每个包重新生成 MVC 框架在指定每个包的 CDNPath 时使用的完全相同的查询字符串来解决的。

以下是使用 MVC Web 应用程序模板完成此操作的方法。这是 BundleConfig 类:

public class BundleConfig
    {
        public static void RegisterBundles(BundleCollection bundles)
        {            
            //we have to go ahead and add our Bundles as if there is no CDN involved.
            //this is because the bundle has to already exist in the BundleCollection
            //in order to get the hash that the MVC framework will generate for the
            //querystring. 
            Bundle jsBundle = new ScriptBundle("~/scripts/js3").Include(
                 "~/Scripts/jquery-{version}.js",
                 "~/Scripts/modernizr-*",
                 "~/Scripts/bootstrap.js",
                 "~/Scripts/respond.js");
            bundles.Add(jsBundle);

            Bundle cssBundle = new StyleBundle("~/content/css3").Include(
                      "~/Content/bootstrap.css",
                      "~/Content/site.css");
            bundles.Add(cssBundle);

#if Debug
            bundles.UseCdn = false; 
#else
            bundles.UseCdn = true;
            //grab our base CDN hostname from web.config...
            string cdnHost = ConfigurationManager.AppSettings["CDNHostName"];

            //get the hashes that the MVC framework will use per bundle for 
            //the querystring. 
            string jsHash = GetBundleHash(bundles, "~/scripts/js3");
            string cssHash = GetBundleHash(bundles, "~/content/css3");

            //set up our querystring per bundle for the CDN path. 
            jsBundle.CdnPath = cdnHost + "/scripts/js3?v=" + jsHash;
            cssBundle.CdnPath = cdnHost + "/content/css3?v=" + cssHash;
#endif
        }

        //Frison B Alexander's code:
        private static string GetBundleHash(BundleCollection bundles, string bundlePath)
        {

            //Need the context to generate response
            var bundleContext = new BundleContext(new HttpContextWrapper(HttpContext.Current), BundleTable.Bundles, bundlePath);

            //Bundle class has the method we need to get a BundleResponse
            Bundle bundle = BundleTable.Bundles.GetBundleFor(bundlePath);
            var bundleResponse = bundle.GenerateBundleResponse(bundleContext);

            //BundleResponse has the method we need to call, but its marked as
            //internal and therefor is not available for public consumption.
            //To bypass this, reflect on it and manually invoke the method
            var bundleReflection = bundleResponse.GetType();

            var method = bundleReflection.GetMethod("GetContentHashCode", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

            //contentHash is whats appended to your url (url?###-###...)
            var contentHash = method.Invoke(bundleResponse, null);
            return contentHash.ToString(); 
        }
    }

关于asp.net-mvc - MVC 5 捆绑和 Azure CDN(查询字符串),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35543576/

相关文章:

c# - Azure 资源管理器 - backupLongTermRetentionPolicies - 错误 "WeekOfYear is required to be set between 1 and 52 in order to set yearly retention."

c++ - 需要缓存友好的数据结构来将字母的邻居存储在二维数组中

mysql - 如何减少浏览器的负载?

java - 如何清除 picasso 的缓存?

c# - 如何在 MVC 5、C# 的 PartialView Controller 中使用 OutputCache?

c# - Url.PathAndQuery 出现错误

Azure VM - 将 *.cloudapp.azure.com URL 链接到 godaddy?

azure - Windows Azure 上多个网站的通配符 SSL 证书

asp.net-mvc - 我可以为 MVC 的 HTML 助手使用 LambdaExpression 类型吗?

c# - 数据存储查询数组