MVC bundle with Azure CDN - how to enable caching

青春壹個敷衍的年華 提交于 2019-12-11 04:08:50

问题


I have a web site to be hosted in Azure that has a lot of javascript and CSS but very small pages. I would like to have the javascript and the CSS delivered via a CDN.

Azure provides a really neat and convenient mechanism to allow this as described here https://azure.microsoft.com/en-us/documentation/articles/cdn-cloud-service-with-cdn/#integrate-aspnet-bundling-and-minification-with-azure-cdn

In short, you add the following code to your BundleConfig.cs

bundles.UseCdn = true;

var version = System.Reflection.Assembly.GetAssembly(typeof(Controllers.HomeController))
    .GetName().Version.ToString();
var cdnUrl = "http://axxxxxx6.vo.msecnd.net/{0}?v=" + version;


ScriptBundle scriptBundle = new ScriptBundle("~/bundles/xx", string.Format(cdnUrl, "bundles/xx"));

scriptBundle.Include(
            "~/Scripts/modernizr-*",
            "~/Scripts/jquery-{version}.js",
            "~/Scripts/jquery.signalR-{version}.js",
            "~/Scripts/jquery.watermark.js", ....

I have followed the instructions to the letter and on the surface it appears to work exactly as expected.

But I realised that the caching for these CDN provided resources is disabled. Every time the web page is requested the JS and the CSS are downloaded again - which defeats the purpose of the CDN altogether.

I have also included the following in the web.config

    <staticContent>
       <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="15.00:00:00"/>
    </staticContent>

What am I missing here?

Thanks in advance.

Dave A

来源:https://stackoverflow.com/questions/33053933/mvc-bundle-with-azure-cdn-how-to-enable-caching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!