How can we enable caching for Bundles in MVC5

不羁岁月 提交于 2019-12-31 04:05:09

问题


I have created 2 bundles in my mvc project as given below:

 public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/Scripts/BaseScripts").Include(
            "~/Content/js/jquery-{version}.js",
            "~/Content/js/jquery-ui-{version}.js",
            "~/Scripts/jquery.unobtrusive-ajax.min.js",
            "~/Content/js/bootstrap.js",
             "~/Content/js/bootstrap-datepicker.js",
              "~/Scripts/jquery.validate.min.js",
              "~/Scripts/jquery.validate.unobtrusive.js",
              "~/Scripts/customvalidation.js"

            )
        );

        bundles.Add(new ScriptBundle("~/Scripts/CustomScripts").Include(
                        "~/Content/js/customscripts/commonscript.js",
          "~/Content/js/customscripts/registration.js"
        ));

        bundles.Add(new StyleBundle("~/Styles/Css").Include(
            "~/Content/css/bootstrap.min.css",
            "~/Content/css/ymobiz.css",
            "~/Content/css/Style.css",
            "~/Content/css/datepicker3.css",
            "~/Content/font-awesome-4.1.0/css/font-awesome.min.css",
            "~/Content/css/css.css"
            ));


        //To enable bundling and minification
        BundleTable.EnableOptimizations = true;
    }

Now i need to enable caching for these files as well. Is there any way by which we can control caching duration for these bundles files


回答1:


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.




回答2:


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")


来源:https://stackoverflow.com/questions/28275725/how-can-we-enable-caching-for-bundles-in-mvc5

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