ASP.NET 应用程序中的 Http 1.1 Headers 配置
Http 1.1 Headers configuration in ASP.NET Application
我这里有一个小问题。这是我的 web.config 的样子:
<add name="Cache-Control" value="no-cache, no-store, must-revalidate, private" />
<add name="Pragma" value="no-cache" />
但是当我检查 ZAP 工具时,我有这样的事情:
Cache-Control: no-cache,no-cache, no-store, must-revalidate, private
Pragma: no-cache,no-cache
因此 pragma 中的值是重复的,在某些响应中 Cache-Control 开头也有 "public",例如:
Cache-Control: public, no-cache, no-cache, no-store, must-revalidate, private
有什么方法可以只从我的 web.config 设置 headers 吗?
另一个问题是有什么方法可以在主响应中设置 headers 但在响应为 .css 和 .js 文件时禁用它们?我希望它们被缓存。
我认为 Cache-Control header 也由 ASP.NET 的 Output Caching 基础设施设置。
我不建议通过手动设置 Cache-Control header 来对抗 ASP.NET...您需要 ASP.NET 的输出缓存基础架构为您完成.可以在 web.config 的 outputCache
element. More information about configuring the output cache is available here.
中配置输出缓存
而且您可能不必太担心 Pragma
header(除非您期待很多来自 1990 年代的 pre-HTTP 1.1 客户!)。
Headers 的静态 js/css 内容通常由 IIS 直接处理,因为静态内容(通常)不是由 ASP.NET 提供的。 This discussion 会让你指向正确的方向。
首先 - 重复不应该成为问题。
HTTP RFC2616 说:
Multiple message-header fields with the same field-name MAY be present
in a message if and only if the entire field-value for that header
field is defined as a comma-separated list [i.e., #(values)]. It MUST
be possible to combine the multiple header fields into one
"field-name: field-value" pair, without changing the semantics of the
message, by appending each subsequent field-value to the first, each
separated by a comma. The order in which header fields with the same
field-name are received is therefore significant to the interpretation
of the combined field value, and thus a proxy MUST NOT change the
order of these field values when a message is forwarded So, multiple
headers with the same name is ok (www-authenticate is such a case) if
the entire field-value is defined as a comma-separated list of values.
您可以找到更多信息 here。
关于具有特定文件扩展名的文件的缓存设置,您可以查看 IIS 中的输出缓存部分。
我找到了适合我的解决方案。
我创建了属性并将其添加到基本控制器:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class NoCacheAttribute : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
filterContext.HttpContext.Response.Cache.AppendCacheExtension("no-store, must-revalidate, private");
base.OnResultExecuting(filterContext);
}
}
[NoCache]
public class BaseController : Controller
现在我所有的 .js、.css、.png、.jpg 文件都被缓存了,但我的请求在缓存中不可见:)
我这里有一个小问题。这是我的 web.config 的样子:
<add name="Cache-Control" value="no-cache, no-store, must-revalidate, private" />
<add name="Pragma" value="no-cache" />
但是当我检查 ZAP 工具时,我有这样的事情:
Cache-Control: no-cache,no-cache, no-store, must-revalidate, private
Pragma: no-cache,no-cache
因此 pragma 中的值是重复的,在某些响应中 Cache-Control 开头也有 "public",例如:
Cache-Control: public, no-cache, no-cache, no-store, must-revalidate, private
有什么方法可以只从我的 web.config 设置 headers 吗?
另一个问题是有什么方法可以在主响应中设置 headers 但在响应为 .css 和 .js 文件时禁用它们?我希望它们被缓存。
我认为 Cache-Control header 也由 ASP.NET 的 Output Caching 基础设施设置。
我不建议通过手动设置 Cache-Control header 来对抗 ASP.NET...您需要 ASP.NET 的输出缓存基础架构为您完成.可以在 web.config 的 outputCache
element. More information about configuring the output cache is available here.
而且您可能不必太担心 Pragma
header(除非您期待很多来自 1990 年代的 pre-HTTP 1.1 客户!)。
Headers 的静态 js/css 内容通常由 IIS 直接处理,因为静态内容(通常)不是由 ASP.NET 提供的。 This discussion 会让你指向正确的方向。
首先 - 重复不应该成为问题。
HTTP RFC2616 说:
Multiple message-header fields with the same field-name MAY be present in a message if and only if the entire field-value for that header field is defined as a comma-separated list [i.e., #(values)]. It MUST be possible to combine the multiple header fields into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field-value to the first, each separated by a comma. The order in which header fields with the same field-name are received is therefore significant to the interpretation of the combined field value, and thus a proxy MUST NOT change the order of these field values when a message is forwarded So, multiple headers with the same name is ok (www-authenticate is such a case) if the entire field-value is defined as a comma-separated list of values.
您可以找到更多信息 here。
关于具有特定文件扩展名的文件的缓存设置,您可以查看 IIS 中的输出缓存部分。
我找到了适合我的解决方案。
我创建了属性并将其添加到基本控制器:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class NoCacheAttribute : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
filterContext.HttpContext.Response.Cache.AppendCacheExtension("no-store, must-revalidate, private");
base.OnResultExecuting(filterContext);
}
}
[NoCache]
public class BaseController : Controller
现在我所有的 .js、.css、.png、.jpg 文件都被缓存了,但我的请求在缓存中不可见:)