3

I want to cache the home page in the client browser

But after setting the ResponseCache attribute, the response header is set to Cache-Control: no-cache, no-store

After reading Response cache not working in asp.net core project and ResponseCache not working in net core 3.1 I can't fix this

[ResponseCache(Duration = 300)]
public class HomeModel : PageModel
{

    public const string PageName = "Home";

    public void OnGet()
    {
    }
}

CacheControl

Mehdi
  • 903
  • 3
  • 10
  • 26

1 Answers1

8

Because I add a form in _layout.cshtml for search field an anti forgery token generated in it automatically!

ASP.net core set Cache-Control: no-cache, no-store for each page which contains an anti forgery field or has Authorize attribute!

By moving this form from the _layout.cshtml, problem resolved!

Mehdi
  • 903
  • 3
  • 10
  • 26
  • For me the problem was that I was using `Html.BeginForm()`, which generated the anti-forgery thing. I switched to an overload of `BeginForm` where I could specify `false` for the `antiforgery` parameter. – RobSiklos Dec 01 '21 at 22:23