@@ -20,6 +20,7 @@ namespace WebApi.OutputCache.V2
2020 [ AttributeUsage ( AttributeTargets . Method , AllowMultiple = false , Inherited = true ) ]
2121 public class CacheOutputAttribute : ActionFilterAttribute
2222 {
23+ private const string CurrentRequestMediaType = "CacheOutput:CurrentRequestMediaType" ;
2324 protected static MediaTypeHeaderValue DefaultMediaType = new MediaTypeHeaderValue ( "application/json" ) { CharSet = Encoding . UTF8 . HeaderName } ;
2425
2526 /// <summary>
@@ -62,8 +63,6 @@ public class CacheOutputAttribute : ActionFilterAttribute
6263 /// </summary>
6364 public Type CacheKeyGenerator { get ; set ; }
6465
65- private MediaTypeHeaderValue _responseMediaType ;
66-
6766 // cache repository
6867 private IApiOutputCache _webApiCache ;
6968
@@ -138,8 +137,9 @@ public override void OnActionExecuting(HttpActionContext actionContext)
138137
139138 var cacheKeyGenerator = config . CacheOutputConfiguration ( ) . GetCacheKeyGenerator ( actionContext . Request , CacheKeyGenerator ) ;
140139
141- _responseMediaType = GetExpectedMediaType ( config , actionContext ) ;
142- var cachekey = cacheKeyGenerator . MakeCacheKey ( actionContext , _responseMediaType , ExcludeQueryStringFromCacheKey ) ;
140+ var responseMediaType = GetExpectedMediaType ( config , actionContext ) ;
141+ actionContext . Request . Properties [ CurrentRequestMediaType ] = responseMediaType ;
142+ var cachekey = cacheKeyGenerator . MakeCacheKey ( actionContext , responseMediaType , ExcludeQueryStringFromCacheKey ) ;
143143
144144 if ( ! _webApiCache . Contains ( cachekey ) ) return ;
145145
@@ -184,10 +184,12 @@ public override async Task OnActionExecutedAsync(HttpActionExecutedContext actio
184184 var cacheTime = CacheTimeQuery . Execute ( DateTime . Now ) ;
185185 if ( cacheTime . AbsoluteExpiration > DateTime . Now )
186186 {
187- var config = actionExecutedContext . Request . GetConfiguration ( ) . CacheOutputConfiguration ( ) ;
187+ var httpConfig = actionExecutedContext . Request . GetConfiguration ( ) ;
188+ var config = httpConfig . CacheOutputConfiguration ( ) ;
188189 var cacheKeyGenerator = config . GetCacheKeyGenerator ( actionExecutedContext . Request , CacheKeyGenerator ) ;
189190
190- var cachekey = cacheKeyGenerator . MakeCacheKey ( actionExecutedContext . ActionContext , _responseMediaType , ExcludeQueryStringFromCacheKey ) ;
191+ var responseMediaType = actionExecutedContext . Request . Properties [ CurrentRequestMediaType ] as MediaTypeHeaderValue ?? GetExpectedMediaType ( httpConfig , actionExecutedContext . ActionContext ) ;
192+ var cachekey = cacheKeyGenerator . MakeCacheKey ( actionExecutedContext . ActionContext , responseMediaType , ExcludeQueryStringFromCacheKey ) ;
191193
192194 if ( ! string . IsNullOrWhiteSpace ( cachekey ) && ! ( _webApiCache . Contains ( cachekey ) ) )
193195 {
0 commit comments