【问题标题】:How to cache css,js or images files to asp.net core如何将 css、js 或图像文件缓存到 asp.net 核心
【发布时间】:2016-06-08 15:08:07
【问题描述】:

间歇性代理导致我的页面被我部署的 asp.net 核心站点缓存。 Web 服务器没有缓存页面。

我添加了请求和响应缓存以防止此代理导致的任何缓存

在我的Startup.cs

        app.UseStaticFiles();

        app.UseIdentity();

        app.Use(async (context, next) =>
        {
            context.Response.Headers.Append("Cache-Control", "no-cache");
            context.Response.Headers.Append("Cache-Control", "private, no-store");
            context.Request.Headers.Append("Cache-Control", "no-cache");
            context.Request.Headers.Append("Cache-Control", "private, no-store");
            await next();
        });

我可以在 fiddler 中看到这些没有缓存标头已添加到我的页面以及 javascript 文件、css 文件和图像文件中。

    1234563
  1. 有没有办法让对 css 和 js 文件的 HTTP 请求不检查每个请求的服务器上是否存在该文件,而仅使用浏览器版本来首次获取这些文件。我问的原因是,在重负载(1000 个用户)时,我在 Fiddler 中发现我的 css、js 和图像文件的 HTTPGET 出现 404 错误,因此我试图限制对这些资源的请求数量。当请求成功(未加载)时,我得到 304 响应(未修改)。有没有办法告诉浏览器首先不要发出请求并使用本地缓存版本。

【问题讨论】:

  • 我不确定我是否理解 2)。你能重述这个问题的第一部分吗?
  • 我重申了这个问题。
  • 谢谢!我根据更新后的问题更新了我的答案。

标签: asp.net-core asp.net-core-mvc


【解决方案1】:
app.UseStaticFiles(new StaticFileOptions()
{
    OnPrepareResponse =
        r =>
        {
            string path = r.File.PhysicalPath;
            if (path.EndsWith(".css") || path.EndsWith(".js") || path.EndsWith(".gif") || path.EndsWith(".jpg") || path.EndsWith(".png") || path.EndsWith(".svg"))
            {
                TimeSpan maxAge = new TimeSpan(7, 0, 0, 0);
                r.Context.Response.Headers.Append("Cache-Control", "max-age=" + maxAge.TotalSeconds.ToString("0"));
            }
        }
});

【讨论】:

    【解决方案2】:

    这是一种适合您的方法。此示例将 css 和 js 文件设置为由浏览器缓存 7 天,并将非 css、js 和图像设置为没有缓存头。另一个注意事项是,只需要在响应上设置缓存控制标头是 Asp.NET Core。

            app.Use(async (context, next) =>
            {
                string path = context.Request.Path;
    
                if(path.EndsWith(".css") || path.EndsWith(".js")) {
    
                    //Set css and js files to be cached for 7 days
                    TimeSpan maxAge = new TimeSpan(7, 0, 0, 0);     //7 days
                    context.Response.Headers.Append("Cache-Control", "max-age="+ maxAge.TotalSeconds.ToString("0")); 
    
                } else if(path.EndsWith(".gif") || path.EndsWith(".jpg") || path.EndsWith(".png")) {
                    //custom headers for images goes here if needed
    
                } else {
                    //Request for views fall here.
                    context.Response.Headers.Append("Cache-Control", "no-cache");
                    context.Response.Headers.Append("Cache-Control", "private, no-store");
    
                }
                await next();
            });
    

    【讨论】:

      【解决方案3】:

      通过 app.UseStaticFiles 的另一种解决方案

       app.UseStaticFiles(new StaticFileOptions
              {
                  OnPrepareResponse = ctx =>
                  {
                      const int durationInSeconds = 60 * 60 * 24;
                      ctx.Context.Response.Headers[HeaderNames.CacheControl] =
                          "public,max-age=" + durationInSeconds;
                  }
              });
      

      你必须添加这个库;

      using Microsoft.Net.Http.Headers;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-07
        • 1970-01-01
        • 2022-01-23
        • 1970-01-01
        • 2011-03-24
        • 2011-10-04
        相关资源
        最近更新 更多