我尝试在 asp.net core 项目中实现响应缓存,但它不起作用。这是 startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddResponseCaching();
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseResponseCaching();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
Run Code Online (Sandbox Code Playgroud)
这是我的控制器。
[ResponseCache(Duration = 30)]
public IActionResult Index()
{
ViewBag.IsMobile = RequestExtensions.IsMobileBrowser(ContextAccessor.HttpContext.Request);
return View();
}
Run Code Online (Sandbox Code Playgroud)
但仍然缓存控制标头 id
cache-control ?no-cache, no-store
Run Code Online (Sandbox Code Playgroud)
我缺少的地方请帮助。此响应缓存不起作用,我从 Microsoft 文档中获取指导
我尝试了解 .NET Core 3.1 中的响应缓存。但它并没有如我所愿。我在 Chrome devtool 中查看了网络,它显示了带有cache-control: no-cache, no-store.
我还发现 Response headerHeaderCacheControl{public,max-age=100}在 Actionfilter 中。这是我期望的值,但浏览器中的实际响应标头是no-cache.
Startup类\xef\xbc\x9a
public void ConfigureServices(IServiceCollection services)\n{\n services.AddResponseCaching(options=> \n {\n options.SizeLimit = 1024;\n options.MaximumBodySize = 1024 * 1024 * 100;\n options.UseCaseSensitivePaths = false;\n });\n}\n\npublic void Configure(IApplicationBuilder app, IWebHostEnvironment env)\n{ \n if (env.IsDevelopment())\n {\n app.UseDeveloperExceptionPage();\n }\n app.UseCookiePolicy();\n app.UseStaticFiles();\n app.UseRouting();\n app.UseAuthentication();\n app.UseAuthorization();\n app.UseResponseCaching();\n app.UseEndpoints(endpoints =>\n {\n endpoints.MapControllerRoute(\n name: "default", \n pattern: "{controller=Home}/{action=Index}/{id?}");\n });\n}\nRun Code Online (Sandbox Code Playgroud)\n控制器:
\n[ResponseCache(Duration = 100, …Run Code Online (Sandbox Code Playgroud)