diff --git a/src/WebApi.OutputCache.V2/CacheOutputAttribute.cs b/src/WebApi.OutputCache.V2/CacheOutputAttribute.cs index ee6fbca..dc69752 100644 --- a/src/WebApi.OutputCache.V2/CacheOutputAttribute.cs +++ b/src/WebApi.OutputCache.V2/CacheOutputAttribute.cs @@ -176,12 +176,17 @@ public override void OnActionExecuting(HttpActionContext actionContext) var val = _webApiCache.Get(cachekey); if (val == null) return; - var contenttype = _webApiCache.Get(cachekey + Constants.ContentTypeKey) ?? new MediaTypeHeaderValue(cachekey.Split(new[] { ':' }, 2)[1].Split(';')[0]); + MediaTypeHeaderValue contentType; + var cachedContentType = _webApiCache.Get(cachekey + Constants.ContentTypeKey); + if (!MediaTypeHeaderValue.TryParse(cachedContentType, out contentType)) + { + contentType = new MediaTypeHeaderValue(cachekey.Split(new[] { ':' }, 2)[1].Split(';')[0]); + } actionContext.Response = actionContext.Request.CreateResponse(); actionContext.Response.Content = new ByteArrayContent(val); - actionContext.Response.Content.Headers.ContentType = contenttype; + actionContext.Response.Content.Headers.ContentType = contentType; var responseEtag = _webApiCache.Get(cachekey + Constants.EtagKey); if (responseEtag != null) SetEtag(actionContext.Response, responseEtag); @@ -214,7 +219,7 @@ public override async Task OnActionExecutedAsync(HttpActionExecutedContext actio if (responseContent != null) { var baseKey = config.MakeBaseCachekey(actionExecutedContext.ActionContext.ControllerContext.ControllerDescriptor.ControllerType.FullName, actionExecutedContext.ActionContext.ActionDescriptor.ActionName); - var contentType = responseContent.Headers.ContentType; + var contentType = responseContent.Headers.ContentType.ToString(); string etag = actionExecutedContext.Response.Headers.ETag.Tag; //ConfigureAwait false to avoid deadlocks var content = await responseContent.ReadAsByteArrayAsync().ConfigureAwait(false);