Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 234
JJJ Avatar asked Aug 23 '10 19:08

JJJ


2 Answers

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)

like image 121
StuartLC Avatar answered Sep 22 '22 06:09

StuartLC


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/.

like image 23
Kris van der Mast Avatar answered Sep 20 '22 06:09

Kris van der Mast