Click here to Skip to main content
11,926,805 members (56,125 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


7 bookmarked

Setting Cache Control HTTP Headers in Web API Controller Method

, 13 Nov 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
The Web API framework has a number of support objects that simplify the job.

Controlling caching behaviour of an HTTP response is an important task that cannot be ignored as it will have a big impact on a web application load and performance. In ASP.NET Web Forms and MVC applications it usually is done by using special directives or attributes that take care of adding corresponding headers to the response. ASP.NET Web API framework unfortunately does not come with an out of the box support for this functionality so concious developers need to deal with that themselves. Luckily it is not a difficult task to do as the Web API framework has a number of support objects that simplify the job.

Basically in order to control caching behaviour we need to make sure that the output response will have what is called "Cache control header" with proper values that determine caching behaviour. Example below demonstrates how to make a response publicly cacheable for a period of time:

var response = new HttpResponseMessage();
response.Headers.CacheControl = new CacheControlHeaderValue
          {Public = true, MaxAge = TimeSpan.FromSeconds(maxAge)};

To simplify the usage further we can even create a static extension method that can be easily applied in a controller method:

// somewhere in a static class
public static HttpResponseMessage PublicCache(this HttpResponseMessage response, int maxAge)
   response.Headers.CacheControl = new CacheControlHeaderValue
          {Public = true, MaxAge = TimeSpan.FromSeconds(maxAge)};
   return response;
// inside an ApiController class
public HttpResponseMessage MyApiMethod(long id)
   var response = new HttpResponseMessage();
   return response.PublicCache(24 * 7 * 60);


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Alexander Turlov
Canada Canada
Alexander Turlov has been working in IT industry since 1987. His programming experience includes such languages as FORTRAN, Pascal, and Basic, C, C++ and C#. He's been working for different industries like science, manufacturing, retail, utilities, finance, insurance, health care, education and so on. His area of interests is rich web applications development with .NET, C#, ASP.NET/MVC and JavaScript. He is working in software development doing architecture, design and development on .NET platform and using Microsoft Visual Studio, IIS and SQL Server as his primary tools, and a lot of other commercial and open source frameworks and tools. He holds a M.Sc. degree in physics and an MCSD.NET certification.

View my profile on LinkedIn

View my blog

You may also be interested in...

Comments and Discussions

SuggestionSolution with Strongly types response Pin
LiQuick10-Sep-14 0:59
memberLiQuick10-Sep-14 0:59 
QuestionVery good article Pin
Volynsky Alex14-Nov-13 12:24
memberVolynsky Alex14-Nov-13 12:24 
AnswerRe: Very good article Pin
Raj_coder17-Mar-14 4:09
memberRaj_coder17-Mar-14 4:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 13 Nov 2013
Article Copyright 2013 by Alexander Turlov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid