How can we enable caching for Bundles in MVC5

点点圈 提交于 2019-12-02 05:09:59

MVC bundles are returned as a single static file to browsers whose cache time is set to 1 year by default. ASP.NET MVC takes care of change tracking of your bundle files and changes bundle url if content of any file is changed or a file is being added / removed from bundle.

As bundles are already cached and change tracking is maintained by asp.net mvc framework, what else cache control do you want on those bundles?

EDIT (in response to comment)

Unfortunately you can not alter that limit. Cache limit is handles by ProcessRequest method of BundleHandler class and this is internal sealed so there is no chance that you can inherit \ override these requests.

For further details you can refer this question.

Thomson Kattingal

Add a key in webconfig

<appSettings>  
<add key="Version" value="sa291988" />  
</appSettings>

Create a class, where we define the format for both JavaScript and styles. using System.Configuration;

namespace BundlingSample   
{  
public class SiteKeys {  
    public static string StyleVersion {  
        get {  
            return "<link href=\"{0}?v=" + 
ConfigurationManager.AppSettings["version"] + "\" rel=\"stylesheet\"/>";  
        }  
    }  
    public static string ScriptVersion {  
        get {  
            return "<script src=\"{0}?v=" + 
ConfigurationManager.AppSettings["version"] + "\"></script>";  
        }  
    }  
}  
}

@Styles.RenderFormat(SiteKeys.StyleVersion,"~/Content/css") 
@Scripts.RenderFormat(SiteKeys.ScriptVersion,"~/bundles/jquery")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!