What\'s the best way to set cache control headers for public caching servers in WebAPI?
I\'m not interested in OutputCache control on my server, I\'m looking to co
As suggested in the comments, you can create an ActionFilterAttribute. Here's a simple one that only handles the MaxAge property:
public class CacheControlAttribute : System.Web.Http.Filters.ActionFilterAttribute
{
public int MaxAge { get; set; }
public CacheControlAttribute()
{
MaxAge = 3600;
}
public override void OnActionExecuted(HttpActionExecutedContext context)
{
if (context.Response != null)
context.Response.Headers.CacheControl = new CacheControlHeaderValue()
{
Public = true,
MaxAge = TimeSpan.FromSeconds(MaxAge)
};
base.OnActionExecuted(context);
}
}
Then you can apply it to your methods:
[CacheControl(MaxAge = 60)]
public string GetFoo(int id)
{
// ...
}