The subtle answer is no.
The explained answer is as follows:
The reason that the output cache does not play good results with cookies
Therefore, the reason the output cache will not cache the response with cookies is because the cookie may be user-specific (e.g. authentication, analytic tracking, etc.). If one or more cookies with the property HttpCookie.Shareable = false , then the output cache considers the answer incompatible.
Decision:
There are several workarounds. The output cache caches response headers and content together and does not provide any intercepts to modify them before sending them back to the user. However, it is possible to provide the ability to modify the cached response headers before they are sent back to the user. One of them requires the Fasterflect nuget package.
I have an example code:
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Caching; using System.Web; using System.Web.Caching; using Fasterflect; namespace CustomOutputCache {
Connect it like this:
<system.web> <caching> <outputCache defaultProvider="HeaderModOutputCacheProvider"> <providers> <add name="HeaderModOutputCacheProvider" type="CustomOutputCache.HeaderModOutputCacheProvider"/> </providers> </outputCache> </caching> </system.web>
And use it like this:
HeaderModOutputCacheProvider.RequestServedFromCache += RequestServedFromCache; HeaderModOutputCacheProvider.RequestServedFromCache += (sender, e) => { e.AddCookies(new HttpCookieCollection { new HttpCookie("key", "value") }); };
I donβt know if he answers your question, but I hope this indicates the right direction.
Barr j
source share