Aspnetcore: Response cache does not work for the view that contain form element

Created on 17 Apr 2018  ·  3Comments  ·  Source: dotnet/aspnetcore

Response cache does not work on the following method which returns view containing a form element.If the form element is removed from the view then response cache works. why it is happening?

```
[Route("Contact")]
[ResponseCache(Duration = 3600)] //Does not working for this method
public IActionResult Contact()
{
return View();
}

[HttpPost]
[Route("Contact")]
public IActionResult Contact(ContactViewModel contactViewModel, IFormFile attachment)
{
    //Necessary opertaion goes here
    return
    View(contactViewModel);
}

```

Most helpful comment

The anti-forgery token in the form is attached to a cookie and neither should be cached or else they could get out of sync.

All 3 comments

Look at the response headers. Is this happening because the form has anti-forgery enabled which always sets no-cache headers?

@Tratcher Yes! anti-forgery was enabled and that's why response cache was not working..My question is why response cache does not work when anti-forgery in enabled?

The anti-forgery token in the form is attached to a cookie and neither should be cached or else they could get out of sync.

Was this page helpful?
0 / 5 - 0 ratings