Why is output caching not working for my ASP.NET MVC 4 app?

独自空忆成欢 提交于 2019-11-30 08:46:30

So, it turns out that OutputCaching was working, it was just that my method of testing it was flawed. The result of an action will only be cached if the response doesn't include a cookie. Of course the first response always includes a cookie if you have ASP.NET Session enabled which we do. Therefore the first response headers look like this:

HTTP/1.1 200 OK
Cache-Control: private, max-age=600
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Expires: Tue, 26 Nov 2013 03:48:44 GMT
Last-Modified: Tue, 26 Nov 2013 03:38:44 GMT
Vary: *
Set-Cookie: ASP.NET_SessionId=kbnhk4lphdlcpozcumpxilcd; path=/; HttpOnly
X-UA-Compatible: IE=Edge
Date: Tue, 26 Nov 2013 03:38:44 GMT
Content-Length: 9558

Assuming your browser or test tool can accept cookies and include those in the subsequent requests, the next request to the same page would result in HTTP response headers like so:

HTTP/1.1 200 OK
Cache-Control: private, max-age=598
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Expires: Tue, 26 Nov 2013 03:48:45 GMT
Last-Modified: Tue, 26 Nov 2013 03:38:45 GMT
Vary: *
X-UA-Compatible: IE=Edge
Date: Tue, 26 Nov 2013 03:38:45 GMT
Content-Length: 9558

As there is no client specific information in the response the output can now be cached as expected.

So, lesson is when testing output caching use a testing tool that can accept and return cookies in subsequent requests.

We ended up using Jmeter rather than tinyget and everything now works as expected.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!