How to prevent web service results from being cached in an ASP.NET web service?
In my ASP.NET app, I noticed that the results of any web service calls are cached. I don't want any results to be cached, how can I stop the browser from caching the results?
Update:
Here's the proxy code generated by calling the web service URL appending '/js', e.g. /mywebservice.asmx/js
var MyWebService=function() {
MyWebService.initializeBase(this);
this._timeout = 0;
this._userContext = null;
this._succeeded = null;
this._failed = null;
}
MyWebService.prototype={
SomeWebMethod:function(itemID,succeededCallback, failedCallback, userContext) {
return this._invoke(MyWebService.get_path(), 'SomeWebMethod',false,
{itemID:itemID},succeededCallback,failedCallback,userContext); }}
MyWebService.registerClass('MyWebService',Sys.Net.WebServiceProxy);
MyWebService._staticInstance = new MyWebService();
MyWebService.set_path = function(value) { MyWebService._staticInstance._path = value; }
MyWebService.get_path = function() { return MyWebService._staticInstance._path; }
MyWebService.set_timeout = function(value) { MyWebService._staticInstance._timeout = value; }
MyWebService.get_timeout = function() { return MyWebService._staticInstance._timeout; }
MyWebService.set_defaultUserContext = function(value) { MyWebService._staticInstance._userContext = value; }
MyWebService.get_defaultUserContext = function() { return MyWebService._staticInstance._userContext; }
MyWebService.set_defaultSucceededCallback = function(value) { MyWebService._staticInstance.开发者_开发技巧_succeeded = value; }
MyWebService.get_defaultSucceededCallback = function() { return MyWebService._staticInstance._succeeded; }
MyWebService.set_defaultFailedCallback = function(value) { MyWebService._staticInstance._failed = value; }
MyWebService.get_defaultFailedCallback = function() { return MyWebService._staticInstance._failed; }
MyWebService.set_path("/MyWebService.asmx");
MyWebService.SomeWebMethod= function(itemID,onSuccess,onFailed,userContext)
{MyWebService._staticInstance.SomeWebMethod(itemID,onSuccess,onFailed,userContext); }
I call the service using:
MyWebService.SomeWebMethod(
itemID,
function(sender, e)
{
// do something
},
function(sender, e)
{
// handle failure
});
I use the suggested technique (append a param to the URL with some random value) with others pages to prevent them from being cached, but I'm surprised that I need to use this technique with a web service call considering that they are POST calls by default in ASP.NET 2.0 and shouldn't be cached in the first place.
If you have control over the server, look here for IIS7
On older versions of IIS add a
Cache-Control: no-cache
to the headers for the ASMX / SVC file (or for the whole directory)
how can I stop the browser from caching the results?
Could it be that you're using ASP.NET ajax? If so then I think you mean that the ajax calls get cached by the browser. If so then take a look at this: http://yoavniran.wordpress.com/2010/04/27/ie-caching-ajax-results-how-to-fix/.
When you make a request to a web service, append a unique value to the querystring, such as the current date and time converted to a numeric value, this will ensure that it's treated as a unique request each time and no caching of responses will take place.
Specifically the Javscript Date.getTime() method:
var d = new Date();
var uniqueValue = d.getTicks();
// Now append uniqueValue to the querystring you're calling the webservice with
// and it'll ensure the request is treated as unique by the browsers cache
I guess you are using IE... is a pita regarding cache control.
The cache-control header can be managed on a request by request basis by using the response object. Be aware that you can't modify the response header once you have issued the first byte of response body.
精彩评论