1

使用跨域 WCF 服务,我似乎无法通过 JSONP 响应获得条件缓存。

当请求的 If-None-Match 值与 Etag 匹配时,我希望得到状态代码为 304 Not Modified 且响应正文为空的响应。相反,我得到一个 200 OK 的响应状态代码,正文中有一个垃圾 JSONP 字符串。

网络配置

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
      <authentication mode="None" />
    <caching>
      <outputCache enableOutputCache="true"/>
      <outputCacheSettings>
        <outputCacheProfiles>
          <add name="ResourceSet" location="Server" duration="30" varyByParam="skip; take" varyByHeader="Accept"/>
          <add name="Resource" location="Any" duration="30" varyByParam="" varyByHeader="Accept"/>
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>
  </system.web>

  <system.serviceModel>
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint
          automaticFormatSelectionEnabled="true"
          helpEnabled="true" crossDomainScriptAccessEnabled="true" />
      </webHttpEndpoint>
        <webScriptEndpoint>
            <standardEndpoint crossDomainScriptAccessEnabled="true"></standardEndpoint>
        </webScriptEndpoint>
    </standardEndpoints>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
  </system.serviceModel>
</configuration>

服务方式:

    [WebGet(UriTemplate = "/{key}")]
    public Resource GetResource(string key)
    {
        ValidateArguments(key);

        Resource resource = resourceRepository.Get(ParseResourceKey(key));

        if (resource == null)
            throw new WebFaultException(HttpStatusCode.NotFound);

        WebFaultException(HttpStatusCode.NotModified);
        Request.CheckConditionalRetrieve(resource.Version);

        // If the caller did not provide an ETag (or it was not a match)
        // Set the ETag on the response message
        Response.SetETag(resource.Version);

        return resource;
    }

要求

    GET http://localhost:1128/CannonicalRESTService.svc/1?callback=jsonpCallback HTTP/1.1
    User-Agent: Fiddler
    Host: localhost:1128
    Accept: application/json
    If-None-Match: "9bb40c00-5de9-463e-8b7d-1105c36318c8"

预期响应

    HTTP/1.1 304 Not Modified
    Server: ASP.NET Development Server/10.0.0.0
    Date: Wed, 13 Feb 2013 21:20:05 GMT
    X-AspNet-Version: 4.0.30319
    Content-Length: 15
    ETag: "9bb40c00-5de9-463e-8b7d-1105c36318c8"
    Cache-Control: private
    Content-Type: application/x-javascript
    Connection: Close

实际反应

    HTTP/1.1 200 OK
    Server: ASP.NET Development Server/10.0.0.0
    Date: Wed, 13 Feb 2013 21:20:05 GMT
    X-AspNet-Version: 4.0.30319
    Content-Length: 15
    ETag: "9bb40c00-5de9-463e-8b7d-1105c36318c8"
    Cache-Control: private
    Content-Type: application/x-javascript
    Connection: Close

    jsonpCallback(null,304);

代码是从http://code.msdn.microsoft.com/cannonicalRESTEntity借来的。我所做的只是将 crossDomainScriptAccessEnabled="true" 添加到 web.config。

我很感激你的时间。

4

0 回答 0