当前位置: 首页 > news >正文

[转]ASP.NET Core: Static Files cache control using HTTP Headers

本文转自:https://www.ryadel.com/en/asp-net-core-static-files-cache-control-using-http-headers/

Every seasoned web developer experienced at least once some cache-related issue with static files. One of the most common scenarios is the following: you publish a new version of your web application that seems to work well, then – despite all your debug and tests – your friends, colleagues and/or users are unable to see the new stuff due to the fact that some old CSS or JS file is being still used by their client, despite you are 100% sure you replaced it on the server with a new version.

How can it even be possible? You ask to yourself for a split second, then you quickly realize that you’re hitting one of the simplest, yet most annoying issues of all time: the mere existence of a browser’s and/or proxy’s cache which is still holding the previous version of your updated file.

Until the last few years, this issue was mostly related to CSS and JS files only: however, with the increasing popularity of Single Page Applications and SPA-oriented frameworks such as ReactJSAngularJS and Angular2, is now also affecting the 

 

Using ASP.NET 4 (and below)

If you’re using ASP.NET 4 or earlier, the issue can be easily overcome by adding some lines to the application’s web.config file, such as the following:

Placing this inside the 

Using ASP.NET Core

As you might already know, the new ASP.NET Core’s configuration system has been re-architected from scratch: it doesn’t depend anymore to XML configuration files such as the web.config, so there’s no chance we can use it to control our static files cache.

The new pattern is based upon key/value settings that can be retrieved from a variety of sources, including Json files (such as the 

Initial check

That said, the first thing to do is to make sure that our ASP.NET Core application is loading the appsettings.json file. Open the Startup.cs file and check if class costructor contains the following lines of code or not:

If we started our project using the yeoman generator or any of the ASP.NET Core templates provided by Visual Studio 2015, everything should be already there: otherwise, just add it.

Setting the HTTP Headers for Static Files

Right after that, keep the Startup.cs file open and scroll down until you reach the Configure method and add (or modify) the 

 

Adding the appsettings.json file to the loop

Now that we learned how to change the default caching behaviour, we need to change these static values with some convenient references pointing to the appsettings.json file. That way we won’t have these settings hard-coded into our application sources and we’ll be able to change them using different settings files, such as an  

In order to do that, open the appsettings.json file (create it if it doesn’t already exists) and add the following key/value section (relevant lines highlighted):

 

Replacing values with references

Now we just need to replace the literal string values in the Startup.cs file with a reference to these configuration keys. We can do that in the following way (modified lines are highlighted):

Learning how to use this new configuration pattern can be very useful, as it’s a great way to customize our web application’s settings.

For further informations regarding this topic, we strongly suggest reading the following great posts from the official ASP.NET Core documentation web site:

  • ASP.NET Core Configuration concepts
  • Working with Static Files

That’s it for now: happy cache control!

 

转载于:https://www.cnblogs.com/freeliver54/p/10361109.html

相关文章:

  • VMware HA实战攻略之五VMwareHA测试验收
  • Java的Cloneable接口还有深浅复制
  • WSUS系列之第三版部署
  • 一篇你看了就懂的DNS详解
  • 使用TripleDES算法加密/解密
  • Sql Server函数全解(二)数学函数
  • IT维护部门的绩效考核怎么做?
  • cs20_8-1
  • 恢复Linux系统里被删除的 Ext3文件
  • Python基础之函数
  • Picturing virtual functions
  • C# 预处理指令
  • 安装Asp.net 2.0服务器出现Server Application Unavailabl --zt
  • 解决FirewallD is not running问题
  • 使用Collectd + InfluxDB + Grafana进行JMX监控
  • [译]CSS 居中(Center)方法大合集
  • 《Javascript数据结构和算法》笔记-「字典和散列表」
  • android百种动画侧滑库、步骤视图、TextView效果、社交、搜房、K线图等源码
  • es6(二):字符串的扩展
  • Java Agent 学习笔记
  • java多线程
  • js继承的实现方法
  • laravel 用artisan创建自己的模板
  • Protobuf3语言指南
  • SQLServer之创建显式事务
  • Webpack 4 学习01(基础配置)
  • 看域名解析域名安全对SEO的影响
  • 如何在GitHub上创建个人博客
  • python最赚钱的4个方向,你最心动的是哪个?
  • Semaphore
  • 哈罗单车融资几十亿元,蚂蚁金服与春华资本加持 ...
  • 蚂蚁金服CTO程立:真正的技术革命才刚刚开始
  • ​DB-Engines 12月数据库排名: PostgreSQL有望获得「2020年度数据库」荣誉?
  • ​软考-高级-系统架构设计师教程(清华第2版)【第15章 面向服务架构设计理论与实践(P527~554)-思维导图】​
  • ​总结MySQL 的一些知识点:MySQL 选择数据库​
  • #!/usr/bin/python与#!/usr/bin/env python的区别
  • #我与Java虚拟机的故事#连载16:打开Java世界大门的钥匙
  • (12)目标检测_SSD基于pytorch搭建代码
  • (3)Dubbo启动时qos-server can not bind localhost22222错误解决
  • (30)数组元素和与数字和的绝对差
  • (31)对象的克隆
  • (32位汇编 五)mov/add/sub/and/or/xor/not
  • (html5)在移动端input输入搜索项后 输入法下面为什么不想百度那样出现前往? 而我的出现的是换行...
  • (牛客腾讯思维编程题)编码编码分组打印下标题目分析
  • ****** 二十三 ******、软设笔记【数据库】-数据操作-常用关系操作、关系运算
  • *p=a是把a的值赋给p,p=a是把a的地址赋给p。
  • *setTimeout实现text输入在用户停顿时才调用事件!*
  • . ./ bash dash source 这五种执行shell脚本方式 区别
  • .Net - 类的介绍
  • .Net Core与存储过程(一)
  • .NET 同步与异步 之 原子操作和自旋锁(Interlocked、SpinLock)(九)
  • .net6 webapi log4net完整配置使用流程
  • .net企业级架构实战之7——Spring.net整合Asp.net mvc
  • .NET学习全景图
  • .NET值类型变量“活”在哪?