2012-12-10 112 views
0

我有一个我从jQuery AJAX调用的WebMethod。不管我做什么,我似乎都无法在返回的JSON数据上正确使用缓存。下面是C#的样子:.NET忽略的缓存设置?

[System.Web.Services.WebMethod(CacheDuration=3600)] 
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)] 
public static List<Items> FetchItems() { //code here} 

我在我的web.config增加了对HTTP GET支持:

<webServices> 
    <protocols> 
    <add name="HttpGet"/> 
    </protocols> 
</webServices> 

而这里的jQuery的AJAX调用看起来是这样的:

$.ajax({ 
    url: "/Ajax/ItemAffinity.aspx/FetchItems?ItemID=" + 
     escape($("#SearchSelectedPageID").val()), 
    type: "GET", 
    cache: true, 
    async: true, 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function (data) { 
     AffinityItems = data.d; 
    } 
}); 

无论我做什么,我都无法获得允许缓存的HTTP响应头:

Cache-Control: no-cache 
Content-Length: 918821 
Content-Type: application/json; charset=utf-8 
Date: Mon, 10 Dec 2012 19:40:57 GMT 
Expires: -1 
Pragma: no-cache 
Server: Microsoft-IIS/8.0 
X-Powered-By: ASP.NET 

最后,通过C#设置这些标题似乎并没有对缓存行为有任何影响:

HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(10)); 
HttpContext.Current.Response.Cache.SetValidUntilExpires(true); 
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Private); 

回答