Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
asp.NETMVC:图像、Css文件和;未缓存javascript文件_Javascript_Html_Asp.net_Css_Asp.net Mvc - Fatal编程技术网

asp.NETMVC:图像、Css文件和;未缓存javascript文件

asp.NETMVC:图像、Css文件和;未缓存javascript文件,javascript,html,asp.net,css,asp.net-mvc,Javascript,Html,Asp.net,Css,Asp.net Mvc,为了安全和一致性,我不希望缓存我的页面。 当我点击浏览器上的“后退”按钮时,它应该总是转到服务器获取html 我通过创建以下actionfilter实现了它 public class NoCache : ActionFilterAttribute { public override void OnResultExecuting(ResultExecutingContext filterContext) { filterContext.HttpContext.Res

为了安全和一致性,我不希望缓存我的页面。 当我点击浏览器上的“后退”按钮时,它应该总是转到服务器获取html

我通过创建以下actionfilter实现了它

public class NoCache : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
        filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
        filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        filterContext.HttpContext.Response.Cache.SetNoStore();

        base.OnResultExecuting(filterContext);
    }
}
我将过滤器作为全局过滤器应用于所有内容

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        // Makes sure that cached pages are not served to any browser including chrome
        filters.Add(new NoCache());
    }
问题解决了。但是现在所有的图像、css和javascript文件也没有被缓存。
我如何告诉它缓存它们?

您可以尝试在
web.config
中启用缓存:

元素的
元素指定Internet信息服务(IIS)发送给Web客户端的与缓存相关的HTTP头,这些头控制Web客户端和代理服务器如何缓存IIS返回的内容

例如,
httpExpires
属性指定内容过期的日期和时间,IIS将向响应中添加一个HTTP“
Expires
”头。
httpExpires
属性的值必须是完全格式化的日期和时间,符合
RFC 1123
中的规范。例如:

<configuration>
   <system.webServer>
      <staticContent>
         <clientCache cacheControlMode="UseExpires"
            httpExpires="Tue, 19 Jan 2038 03:14:07 GMT" />
      </staticContent>
   </system.webServer>
</configuration>

你可以参考更多信息