防止使用ServiceStack返回304 Not Modified时出现不必要的头信息

6
使用ServiceStack,我只想要返回304未修改,如下所示:
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-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)
  {
  }
}
2个回答

8
ServiceStack只会发出在EndpointHostConfig.GlobalResponseHeaders中注册的标头。
如果您不希望它们被发送,请将它们删除,例如:
SetConfig(new EndpointHostConfig { 
    GlobalResponseHeaders = new Dictionary<string,string>()
});

您可以使用HttpResult来临时添加它们,例如:
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和CORS


谢谢你的回答,但这只是移除了CORS,而没有移除其他的。我仍然得到了HTTP/1.1 304 Not Modified Content-Length: 0 Content-Type: application/json Expires: Thu, 16 Aug 2012 16:20:25 GMT Server: Microsoft-HTTPAPI/2.0 Date: Thu, 16 Aug 2012 16:20:24 GMT - Martin Tapp
未在GlobalResponseHeaders中或使用HttpResult逐个服务添加的发射标头不属于ServiceStack。IIS / ASP.NET可能会选择添加自己的标头。 - mythz
不使用IIS/ASP.Net,只是控制台应用程序测试。似乎是从JSON序列化器和其他地方添加的。我如何控制这些? - Martin Tapp
您确定设置了 GlobalResponseHeaders = new Dictionary<string,string>() 吗?这将删除 ServiceStack 标头,但不会添加任何序列化器。我不知道如何删除 HttpListener 添加的 Server HTTP 标头。 - mythz
HTTP规范指出,某些状态码需要Content-Type,而不是所有状态码。我尝试使用自己的Content-Type,但没有成功(与请求的内容不同,仅控制响应内容,即不包括头部)。在ServiceStack或ASP.NET/IIS中,我可以拦截什么来控制HTTP消息(头部+内容)? - Martin Tapp
显示剩余4条评论

0

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

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

不会返回ContentType、ContentLength等内容


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接