Adding Cache headers to OData calls

For every request, the OData framework calls OnStartProcessingRequest method. We can take advantage of this call to add various cache headers.

protected override void OnStartProcessingRequest(ProcessRequestArgs args)
{
    base.OnStartProcessingRequest(args);
    HttpContext context = HttpContext.Current;
    HttpCachePolicy c = HttpContext.Current.Response.Cache;
    c.SetCacheability(HttpCacheability.ServerAndPrivate);
    c.SetExpires(HttpContext.Current.Timestamp.AddSeconds(60));
    c.VaryByHeaders["Accept"] = true;
    c.VaryByHeaders["Accept-Charset"] = true;
    c.VaryByHeaders["Accept-Encoding"] = true;
    c.VaryByParams["*"] = true;
}

Enjoyed this post? Share it!