如何将CSS,JS或图像文件缓存到ASP.NET Core [英] How to cache css,js or images files to asp.net core

查看:62
本文介绍了如何将CSS,JS或图像文件缓存到ASP.NET Core的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

An intermittent proxy was causing my pages to get cached with an asp.net core site I deployed. The web server was not caching pages.

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

I added request and response caching to prevent any caching this proxy was causing

在我的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();
        });

我可以在小提琴手中看到,这些高速缓存标头未添加到我的页面以及javascript文件,css文件和图像文件中.

I can see in fiddler these no cache headers have been added to my pages as well as to javascript files, css files and image files.

  1. 如何限制此无缓存标头仅适用于asp.net mvc页面,这样这些无缓存标头就不会出现在提琴手中的js,css和图像文件之类的非页面文件中

  1. How do I limit this no caching headers to only apply to asp.net mvc pages so these no cache headers don't showup in fiddler for non page files like js,css, and image files

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

Is there a way that for HTTP requests for css and js files to not check if the file exists on the server for every request, and rather just the browser version is used for the first get of those files. The reason I ask is that on heavy load (1000 users) I in Fiddler I notice I get 404 errors for HTTPGETs for my css,js and image file so I'm trying to limit the number of requests for those resources. When the requests are successful(not under load) I get 304 responses (not modified). Is there not a way to tell the browser to not make the request in the first place and use the local cached version.

推荐答案

这是一种适合您的方法.本示例将css和js文件设置为由浏览器缓存7天,并将非css,js和图像设置为不具有缓存头.另请注意,Asp.NET Core是仅需在响应上设置缓存控制标头.

Here is an approach that should work for you. This example sets css and js files to be cached for 7 days by the browser and sets non css, js, and images to have no cache headers. One other note, it's only necessary to set the cache control headers on the response is 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();
        });

这篇关于如何将CSS,JS或图像文件缓存到ASP.NET Core的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆