I tried to learn about Response Caching in .NET Core 3.1. But it didn't work as I wished. I viewed the network in Chrome devtool, and it showed the response header with cache-control: no-cache, no-store
.
I also found that Response header was with HeaderCacheControl{public,max-age=100}
in Actionfilter. It was the value I expected, but the actual response header in browser is no-cache
.
Startup
class:
public void ConfigureServices(IServiceCollection services)
{
services.AddResponseCaching(options=>
{
options.SizeLimit = 1024;
options.MaximumBodySize = 1024 * 1024 * 100;
options.UseCaseSensitivePaths = false;
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCookiePolicy();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseResponseCaching();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
Controller:
[ResponseCache(Duration = 100, NoStore = false)]
public IActionResult Index()
{
return View();
}