我有一个我从jQuery AJAX调用的WebMethod。无论我做什么,我似乎无法使缓存在返回的JSON数据上正常工作。以下是C#的外观:
[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);
答案 0 :(得分:1)
这是因为默认情况下不会缓存动态数据。
以下是允许它的自定义HttpHandler的实现:http://www.hackification.com/2009/05/01/forcing-the-browser-to-cache-dynamic-content-in-aspnet/