Max-age settings in ASP. NET 2.0

Source: Internet
Author: User

A bug in ASP. NET 2.0 is that the max-age header cannot be changed. When max-age is set to 0, ASP. NET 2.0 sets Cache-control to private, because max-age = 0 means no Cache is required. Therefore, there is no way to make ASP. NET 2.0 return the cache response header. This is because the ASP. net ajax framework intercepts Web service calls and sets max-age to 0 as the default value before executing a request.

 
 
  1. public string CachedGet2()  
  2. {  
  3. TimeSpan cacheDuration = TimeSpan.FromMinutes(1);  
  4. FieldInfo maxAge = Context.Response.Cache.GetType().GetField("_maxAge",  
  5. BindingFlags.Instance|BindingFlags.NonPublic);  
  6. maxAge.SetValue(Context.Response.Cache, cacheDuration);  
  7. Context.Response.Cache.SetCacheability(HttpCacheability.Public);  
  8. Context.Response.Cache.SetExpires(DateTime.Now.Add(cacheDuration));  
  9. Context.Response.Cache.AppendCacheExtension(  
  10. "must-revalidate, proxy-revalidate");  
  11. return DateTime.Now.ToString();  


Now max-age is set to 60, so the browser will cache the response for 60 seconds. If you make the same call again within 60 seconds, the same response will be returned. The test output here shows the time returned from the server:

One minute later, when the cache expires, the browser sends a request to the server again. The client code is as follows:

 
 
  1. function testCache()  
  2. {  
  3. TestService.CachedGet(function(result)  
  4. {  
  5. debug.trace(result);  
  6. });  


Another problem is solved. In the web. config file, you will see that ASP. NET Ajax has added the following node values:

 
 
  1. <system.web> 
  2. <trust level="Medium"/> 


This prevents us from setting the _ maxAge field of the Response object because it requires reflection. Therefore, you have to delete this level of trust or place it as Full.

 
 
  1. <system.web> 
  2. <trust level="Full"/> 
  1. Analysis of configuration files in ASP. NET
  2. . Net ria Services is as convenient as ASP. NET
  3. Overview of the UpdatePanel control in ASP. net ajax Extensions
  4. ASP. NET calls the UpdatePanel Update () method
  5. Introduction to the ASP. net ajax wcf Service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.