Created
October 27, 2017 02:05
-
-
Save brandedoutcast/ac4c0919f74db2d4769dcb57a89e7c8d to your computer and use it in GitHub Desktop.
Return Cache-Control response header in a Web API response
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CacheFilterAttribute : ActionFilterAttribute | |
{ | |
int DurationInSeconds = int.Parse(ConfigurationManager.AppSettings["CacheDuration"]); | |
public override Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken) | |
{ | |
if (actionExecutedContext.Request.Method == HttpMethod.Get) | |
{ | |
actionExecutedContext.Response.Headers.CacheControl = new CacheControlHeaderValue() | |
{ | |
MaxAge = TimeSpan.FromSeconds(DurationInSeconds), | |
Public = true | |
}; | |
} | |
return Task.FromResult(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment