返回304 Not Modified with ServiceStack时防止不需要的标头

时间:2012-08-07 13:50:50

标签: servicestack http-status-code-304

使用ServiceStack,我只想返回 304 Not Modified

HTTP/1.1 304 Not Modified

但是,ServiceStack会添加许多其他不需要的(返回带有304代码的HttpResult)标头:

HTTP/1.1 304 Not Modified
Content-Length: 0
Content-Type: application/json
Server: Microsoft-HTTPAPI/2.0
X-Powered-By: ServiceStack/3.94 Win32NT/.NET
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Headers: Content-Type
Date: Tue, 07 Aug 2012 13:39:19 GMT

如何防止输出其他标题?我已尝试使用HttpResult的各种方法,注册虚拟内容类型过滤器,但其名称仅表示控件内容,而不是标题或其他列出的here。我也尝试用IStreamWriter和IHasOptions实现我自己的IHttpResult派生,结果相同:ServiceStack添加了不需要的标题。

由于

更新

可以使用以下内容删除content-type,但仍有一些标题,例如content-lengthserverdate

    public override object OnGet(FaultTypes request)
    {
      var result = new HttpResult
      {
       StatusCode = HttpStatusCode.NotModified,
       StatusDescription = "Not Modified", // Otherwise NotModified written!
      };

      // The following are hacks to remove as much HTTP headers as possible
      result.ResponseFilter = new NotModifiedContentTypeWriter();
      // Removes the content-type header
      base.Request.ResponseContentType = string.Empty;

      return result;
    }

class NotModifiedContentTypeWriter : ServiceStack.ServiceHost.IContentTypeWriter
{
  ServiceStack.ServiceHost.ResponseSerializerDelegate ServiceStack.ServiceHost.IContentTypeWriter.GetResponseSerializer(string contentType)
  {
    return ResponseSerializerDelegate;
  }

  void ServiceStack.ServiceHost.IContentTypeWriter.SerializeToResponse(ServiceStack.ServiceHost.IRequestContext requestContext, object response, ServiceStack.ServiceHost.IHttpResponse httpRes)
  {
  }

  void ServiceStack.ServiceHost.IContentTypeWriter.SerializeToStream(ServiceStack.ServiceHost.IRequestContext requestContext, object response, System.IO.Stream toStream)
  {
  }

  string ServiceStack.ServiceHost.IContentTypeWriter.SerializeToString(ServiceStack.ServiceHost.IRequestContext requestContext, object response)
  {
    return string.Empty;
  }

  public void ResponseSerializerDelegate(ServiceStack.ServiceHost.IRequestContext requestContext, object dto, ServiceStack.ServiceHost.IHttpResponse httpRes)
  {
  }
}

2 个答案:

答案 0 :(得分:8)

ServiceStack发出的唯一标头是在EndpointHostConfig.GlobalResponseHeaders中注册的标头。

如果你不想发出它们,请删除它们,例如:

SetConfig(new EndpointHostConfig { 
    GlobalResponseHeaders = new Dictionary<string,string>()
});

您可以使用HttpResult在adhoc基础上添加它们,例如:

return new HttpResult(dto) {
    Headers = {
       { "Access-Control-Allow-Origin", "*" },
       { "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS" } 
       { "Access-Control-Allow-Headers", "Content-Type" }, }
};

这两个选项在servicestack REST API and CORS

中有更详细的解释

答案 1 :(得分:0)

实际上你可以在你的API中做这样的事情

base.Response.StatusCode = (int) HttpStatusCode.NotModified;
base.Response.EndHttpRequestWithNoContent();
return new HttpResult();

哪个不会返回ContentType,ContentLength等

相关问题