[英]asp.net core 3 Response Caching Not Working As Expected
我对 ASP.NET Core 3 比较陌生,并且正在围绕 CMS 构建一个简单的概念证明。 我试图证明我们能够缓存 static 内容(.jpg、.png 等),但防止缓存动态(从数据库传递到模型的内容)。 static 内容似乎运行良好。 然而,即使响应标头似乎设置正确,动态内容也会被缓存。
从 Microsoft Edge 开发人员工具中看到的我的响应标头是:
缓存控制:无缓存,无存储,必须重新验证内容类型:文本/html; charset=utf-8 日期:2020 年 6 月 29 日星期一 18:25:58 GMT 过期:0 编译指示:无缓存服务器:Kestrel 状态:200
在这一点上,我没有使用
app.UseResponseCaching();
我仅将它用于 Startup.cs 中的 static 文件
app.UseStaticFiles(new StaticFileOptions // for wwwroot files
{
OnPrepareResponse = (context) =>
{
var headers = context.Context.Response.GetTypedHeaders();
headers.CacheControl = new CacheControlHeaderValue
{
Public = true,
MaxAge = TimeSpan.FromDays(30)
};
}
});
和这个
services.AddMvc(options =>
{
options.Conventions.Add(new RouteTokenTransformerConvention(
new SlugifyParameterTransformer()));
options.CacheProfiles.Add("Default0", new CacheProfile()
{
NoStore = true,
Location = ResponseCacheLocation.None,
Duration = 0
});
})
...将此添加到我的 Controller class
[ResponseCache(CacheProfileName = "Default0")]
public class CmsController : CmsControllerBase<CmsPageModel>
...而且为了更好的衡量,我也直接将它放在我的视图中:
@using Microsoft.Net.Http.Headers;
@using Microsoft.AspNetCore.Http;
@model CMS.Web.Models.CmsPageModel
@{
this.Context.Response.Headers[HeaderNames.CacheControl] = "no-cache, no-store, must-revalidate";
this.Context.Response.Headers[HeaderNames.Expires] = "0";
this.Context.Response.Headers[HeaderNames.Pragma] = "no-cache";
}
我在浏览器上按了 F5 几次,无论我做什么,相同的缓存内容都会返回,直到我重建应用程序并重新启动它。 有想法的程序员朋友吗?
我从来没有能够明确地弄清楚这一点,但解决方案的形式是从运行代码的目录中删除所有文件,然后重建整个解决方案。 看起来可能有一些旧的缓存相关文件被解决方案抛弃,但显然仍在 position 中以影响应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.