Skip to content

Instantly share code, notes, and snippets.

@brandedoutcast
Created October 27, 2017 02:05
Show Gist options
  • Save brandedoutcast/ac4c0919f74db2d4769dcb57a89e7c8d to your computer and use it in GitHub Desktop.
Save brandedoutcast/ac4c0919f74db2d4769dcb57a89e7c8d to your computer and use it in GitHub Desktop.
Return Cache-Control response header in a Web API response
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