2

这是我无法弄清楚的事情。

我有我希望客户端在浏览器中缓存的大型 JSON 数据集。我正在使用 jquery AJAX 调用 ac# web 方法。

[System.Web.Services.WebMethod]

这是jQuery:

            $.ajax({
                url: "/Ajax/ItemAffinity.aspx/FetchAffinityItems?ItemID=" + escape($("#SearchSelectedPageID").val()),
                type: "POST",
                data: "{}",
                cache: true,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                //contentType: "application/json; charset=utf-8",
                success: function (data) {
                   //do cool stuff
                }
            });

无论我在 web 方法中在服务器上指定什么,HTTP HEADERS 总是看起来像这样:

Cache-Control   no-cache
Content-Length  919527
Content-Type    application/json; charset=utf-8
Expires -1

我放入 web 服务的任何设置都会被立即忽略,如下所示:

        HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(1));
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);

Web 服务不适用于 HTTP GET,对吧?或者我应该怎么做?

谢谢!

4

2 回答 2

1

为了最大程度地控制,您可以像这样设置缓存响应标头:

    <WebMethod()> _
    <ScriptMethod(UseHttpGet:=True)> _
    Public Function get_time() As String

        'Cache the reponse to the client for 60 seconds:
        Dim context As HttpContext = HttpContext.Current
        Dim cacheExpires As New TimeSpan(0, 0, 0, 60)
        SetResponseHeaders(context, HttpCacheability.Public, cacheExpires)

        Return Date.Now.ToString

    End Function

    ''' <summary>
    ''' Sets the headers of the current http context response.
    ''' </summary>
    ''' <param name="context">A reference to the current context.</param>
    ''' <param name="cacheability">The cachability setting for the output.</param>
    ''' <param name="delta">The amount of time to cache the output. Set to Nothing if NoCache.</param>
    ''' <remarks></remarks>
    Public Shared Sub SetResponseHeaders(ByRef context As HttpContext,
                                         cacheability As HttpCacheability,
                                         delta As TimeSpan)

        If Not IsNothing(context) Then
            Dim cache As HttpCachePolicy = context.ApplicationInstance.Response.Cache
            cache.SetCacheability(cacheability)

            Select Case cacheability
                Case HttpCacheability.NoCache
                    'prevent caching:
                    Exit Select
                Case Else
                    'set cache expiry:
                    Dim dateExpires As Date = Date.UtcNow
                    dateExpires = dateExpires.AddMinutes(delta.TotalMinutes)
                    'set expiry date:
                    cache.SetExpires(dateExpires)
                    Dim maxAgeField As Reflection.FieldInfo = cache.GetType.GetField("_maxAge", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
                    If Not IsNothing(maxAgeField) Then
                        maxAgeField.SetValue(cache, delta)
                    End If
            End Select
        End If

    End Sub

然后使用 ajax GET 调用您的 web 服务,如下所示:

var postObj = {
    ItemID: 12
}

$.ajax({
    url: webserviceUrl,
    type: 'GET',
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    data: postObj,
    success: function (reponse) {
        alert(response.d);
    }
});
于 2012-12-30T16:54:16.110 回答
0

WebMethod 属性上有一个属性,您可以设置缓存。我不确定这是否会在标头中设置缓存值。

http://msdn.microsoft.com/en-us/library/byxd99hx%28v=vs.71%29.aspx#vbtskusingwebmethodattributecacheduration

于 2012-12-07T18:35:06.640 回答