I need to make sure that browsers don't cache the response from my web service. One way to do that is to append a querystring variable with a random value. Another way is to have the server set a header of Cache-Control: max-age=0 on each response.
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? The cache-control header is prefered, of course.
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.
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
The downside is that it does not work in Self Host.
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
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).
If you want to set individual headers, you can change your action signature to return HttpResponseMessage instead, and then you can use "Request.CreateResponse" 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.
mattj1856
Member
7 Points
7 Posts
cache control
Mar 27, 2012 01:42 PM|LINK
I need to make sure that browsers don't cache the response from my web service. One way to do that is to append a querystring variable with a random value. Another way is to have the server set a header of Cache-Control: max-age=0 on each response.
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? The cache-control header is prefered, of course.
-Matt
panesofglass
Member
730 Points
237 Posts
Re: cache control
Mar 27, 2012 01:46 PM|LINK
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.
imran_ku07
All-Star
45785 Points
7698 Posts
MVP
Re: cache control
Mar 27, 2012 05:23 PM|LINK
web.config is also an option,
<system.webServer> <httpProtocol> <customHeaders> <add name="cache-control" value="no-cache" /> </customHeaders> </httpProtocol> </system.webServer>Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
panesofglass
Member
730 Points
237 Posts
Re: cache control
Mar 27, 2012 05:31 PM|LINK
I always forget this option. :)
imran_ku07
All-Star
45785 Points
7698 Posts
MVP
Re: cache control
Mar 27, 2012 05:49 PM|LINK
The downside is that it does not work in Self Host.
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: cache control
Mar 27, 2012 09:17 PM|LINK
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).
If you want to set individual headers, you can change your action signature to return HttpResponseMessage instead, and then you can use "Request.CreateResponse" 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.