aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

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

Open tanvirarjel opened this issue 8 years ago • 3 comments

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);
 }

tanvirarjel avatar Apr 17 '18 05:04 tanvirarjel

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

Tratcher avatar Apr 17 '18 16:04 Tratcher

@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?

tanvirarjel avatar Apr 20 '18 15:04 tanvirarjel

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.

Tratcher avatar Apr 20 '18 18:04 Tratcher