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

Mar*_*app 6 http-status-code-304 servicestack

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

HTTP/1.1 304 Not Modified
Run Code Online (Sandbox Code Playgroud)

但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
Run Code Online (Sandbox Code Playgroud)

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

谢谢

更新

能够去除content-type通过使用以下,但有些头依然存在,即content-length,serverdate.

    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)
  {
  }
}
Run Code Online (Sandbox Code Playgroud)

myt*_*thz 8

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

如果您不希望它们被发射,请将它们移除,例如:

SetConfig(new EndpointHostConfig { 
    GlobalResponseHeaders = new Dictionary<string,string>()
});
Run Code Online (Sandbox Code Playgroud)

您可以使用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" }, }
};
Run Code Online (Sandbox Code Playgroud)

这两个选项在更详细地解释:servicestack REST API和CORS