How to cache asmx/js call? I tried with HTTPModule and implemented caching . But this cache value is expiring during next login. It is reset to one year back of the last modified date. Through code I am not doing that.Is there any default setting
in asmx for doing that? If so ,is there a way to override the setting?
if you use Getmethod for requesting, request response will be returned
without call the service as it is cached in the browser history it self. when requests change, the call will reach the service.
MCTS
If you feel it helps, Mark as answered so that it can help others to find solution.
For Any further questions, please contact me.
Actually my requirement is to cache this call (GetMygrid.asmx/js) in browser cache for some days and to avoid 304 response. I am able to cache this call using HTTPModule.
This code works fine during runtime while I am accessing the specific page many times. (GetMygrid.asmx/js) I am not getting 304 response while accessing the page and getting the following cache header.
Cache-Control: public
Expires: Mon, 27 Feb 2012 04:52:11 GMT
Last-Modified: Wed, 22 Feb 2012 04:52:11 GMT
Server: Microsoft-IIS/7.5
But my problem is when I login next time in app, this cache expires and value changes to one year back.I am using GET method for this call.
I am seeing the exact same behaviour after a move to ASP.NET 4 (on IIS 7). Do you by any chance use Spring.Net ?
Edit: Ignore me, this is how I would expect Asp.Net to work (now I've delved into it) be aware that the call to .SetExpires in the above code sample will silently fail on the first call into the module as (it turns out) you can only call 'SetExpires' on
a HttpCachePolicy that hasn't already had that header set (unless you want to set an expires that is older than the current one, apparently that works) .
anoopkp10
Member
14 Points
5 Posts
How to cache asmx/js call
Feb 21, 2012 12:37 PM|LINK
How to cache asmx/js call? I tried with HTTPModule and implemented caching . But this cache value is expiring during next login. It is reset to one year back of the last modified date. Through code I am not doing that.Is there any default setting in asmx for doing that? If so ,is there a way to override the setting?
Muhammad Fak...
Contributor
2258 Points
511 Posts
Re: How to cache asmx/js call
Feb 21, 2012 02:06 PM|LINK
if you use Get method for requesting, request response will be returned without call the service as it is cached in the browser history it self. when requests change, the call will reach the service.
If you feel it helps, Mark as answered so that it can help others to find solution.
For Any further questions, please contact me.
Muhammad Fak...
Contributor
2258 Points
511 Posts
Re: How to cache asmx/js call
Feb 21, 2012 02:09 PM|LINK
This will help you how to write that
http://api.jquery.com/jQuery.get/
If you feel it helps, Mark as answered so that it can help others to find solution.
For Any further questions, please contact me.
anoopkp10
Member
14 Points
5 Posts
Re: How to cache asmx/js call
Feb 22, 2012 04:05 AM|LINK
Actually my requirement is to cache this call (GetMygrid.asmx/js) in browser cache for some days and to avoid 304 response. I am able to cache this call using HTTPModule.
private void EndRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
var path = context.Request.Path.ToLower().Replace('\\', '/');
if (path.EndsWith("/js") || path.EndsWith("/jsdebug") )
{
var lastModified = DateTime.UtcNow;
var expires = lastModified + TimeSpan.FromDays(5);
context.Response.Cache.SetLastModified(lastModified);
context.Response.Cache.SetExpires(expires);
context.Response.Cache.SetCacheability(HttpCacheability.Public);
}
}
This code works fine during runtime while I am accessing the specific page many times. (GetMygrid.asmx/js) I am not getting 304 response while accessing the page and getting the following cache header.
Cache-Control: public
Expires: Mon, 27 Feb 2012 04:52:11 GMT
Last-Modified: Wed, 22 Feb 2012 04:52:11 GMT
Server: Microsoft-IIS/7.5
But my problem is when I login next time in app, this cache expires and value changes to one year back.I am using GET method for this call.
Cache-Control: private
Content-Type: application/x-javascript; charset=utf-8
Content-Encoding: gzip
Expires: Mon, 21 Feb 2011 15:35:48 GMT
Last-Modified: Wed, 22 Feb 2012 04:39:37 GMT
So on next hit I am getting 304 for the same request.Is there a way to avoid that.
Otomii Lu - ...
Contributor
3064 Points
490 Posts
Microsoft
Re: How to cache asmx/js call
Feb 24, 2012 06:59 AM|LINK
As far as I Know, this should not happen.
Try to debug your HTTPModule when you login for the second time,
to make sure EndRequest is executed and the Expires header has the correct value.
javajunky
Member
2 Points
1 Post
Re: How to cache asmx/js call
Mar 01, 2012 03:57 PM|LINK
I am seeing the exact same behaviour after a move to ASP.NET 4 (on IIS 7). Do you by any chance use Spring.Net ?
Edit: Ignore me, this is how I would expect Asp.Net to work (now I've delved into it) be aware that the call to .SetExpires in the above code sample will silently fail on the first call into the module as (it turns out) you can only call 'SetExpires' on a HttpCachePolicy that hasn't already had that header set (unless you want to set an expires that is older than the current one, apparently that works) .
anoopkp10
Member
14 Points
5 Posts
Re: How to cache asmx/js call
Mar 06, 2012 12:28 PM|LINK
My issue solved with the below code change to the above code
context.Response.Cache.SetMaxAge(TimeSpan.FromDays(5));
Thanks for the replies