Exemple #1
0
 private static void CalculateETagAndCopyStreamIfNecessary(HttpResponse resp, HttpRequest req, MemoryStream buffer, Stream stream)
 {
     using (var reader = new StreamReader(buffer))
     {
         if (resp.Headers[HeaderNames.ETag].Count == 0)
         {
             var body = reader.ReadToEnd();
             resp.Headers.Add(HeaderNames.ETag, ETagGenerator.GetETag(req, body));
             if (req.Headers[HeaderNames.IfNoneMatch].Count != 0 && req.Headers[HeaderNames.IfNoneMatch] == resp.Headers[HeaderNames.ETag])
             {
                 resp.StatusCode = StatusCodes.Status304NotModified; // this will blank response
             }
         }
         CopyStreamIfNecessary(resp, buffer, stream);
     }
 }
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var executedContext = await next.Invoke();

            var req  = executedContext.HttpContext.Request;
            var resp = executedContext.HttpContext.Response;

            if (!ValidateMethod(req, resp) || executedContext.Result == null)
            {
                return;
            }
            var eTag = ETagGenerator.GetETag(executedContext.HttpContext.Request, JsonConvert.SerializeObject(executedContext.Result));

            SetCacheHeaderForClientSideCaching(resp, eTag);
            if (CacheLocation == CacheLocation.Client)
            {
                return;
            }
            await CacheService.Store($"{_cacheKey}_{HeaderNames.ETag}", eTag, ServerSideDuration);
        }