0

我有一个从 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);
4

1 回答 1

1

这是因为默认情况下不缓存动态数据。

这是允许它的自定义 HttpHandler 的实现:http ://www.hackification.com/2009/05/01/forcing-the-browser-to-cache-dynamic-content-in-aspnet/

于 2012-12-10T20:03:20.740 回答