cache controlhttp://forums.asp.net/t/1785811.aspx/1?cache+controlTue, 27 Mar 2012 21:17:17 -040017858114901791http://forums.asp.net/p/1785811/4901791.aspx/1?cache+controlcache control <p>I need to make sure that browsers don't cache the response from my web service.&nbsp;&nbsp; One way to do that is to append a querystring variable with a random value.&nbsp; Another way is to have the server set&nbsp;a header of Cache-Control: max-age=0 on each response.</p> <p>I can easly create a message handler to perform either of these operations, but shouldn't there be a simple property set somewhere on the WebApi that does this automatically?&nbsp; The cache-control header is prefered, of course.</p> <p>-Matt</p> 2012-03-27T13:42:12-04:004901801http://forums.asp.net/p/1785811/4901801.aspx/1?Re+cache+controlRe: cache control <p>I wouldn't say a property is the preferred way. I would argue for the message handler approach. It's more a matter of how you would interact with the property. I can't imagine everyone would want one and only one mechanism for setting that for all responses across their apps, which a property -- I suppose on your HttpConfiguration -- would promote. If we had a configuration-per-route, this might be a decent option.</p> 2012-03-27T13:46:13-04:004902255http://forums.asp.net/p/1785811/4902255.aspx/1?Re+cache+controlRe: cache control <p>web.config is also an option,</p> <pre class="prettyprint">&lt;system.webServer&gt; &lt;httpProtocol&gt; &lt;customHeaders&gt; &lt;add name=&quot;cache-control&quot; value=&quot;no-cache&quot; /&gt; &lt;/customHeaders&gt; &lt;/httpProtocol&gt; &lt;/system.webServer&gt;</pre> <p><br> <br> </p> 2012-03-27T17:23:54-04:004902264http://forums.asp.net/p/1785811/4902264.aspx/1?Re+cache+controlRe: cache control <p></p> <blockquote><span class="icon-blockquote"></span> <h4>imran_ku07</h4> <p></p> <p>web.config is also an option,</p> <pre class="prettyprint">&lt;system.webServer&gt; &lt;httpProtocol&gt; &lt;customHeaders&gt; &lt;add name=&quot;cache-control&quot; value=&quot;no-cache&quot; /&gt; &lt;/customHeaders&gt; &lt;/httpProtocol&gt; &lt;/system.webServer&gt;</pre> <p></p> </blockquote> <p></p> <p>I always forget this option. :)</p> 2012-03-27T17:31:43-04:004902296http://forums.asp.net/p/1785811/4902296.aspx/1?Re+cache+controlRe: cache control <p></p> <blockquote><span class="icon-blockquote"></span> <h4>panesofglass</h4> I always forget this option. :)</blockquote> <p></p> <p>The downside is that it does not work in Self Host.</p> <p></p> 2012-03-27T17:49:18-04:004902589http://forums.asp.net/p/1785811/4902589.aspx/1?Re+cache+controlRe: cache control <p>We will probably not have anything like [OutputCache] in the first RTM release (which admittedly does more than setting cache headers, but that is one of the side-effects of it).</p> <p>If you want to set individual headers, you can change your action signature to return HttpResponseMessage instead, and then you can use &quot;Request.CreateResponse&quot; as a shortcut to easily create a response with a serialized (and content-type negotiated) object in it; then you can tweak the headers as you see fit.</p> 2012-03-27T21:17:17-04:00