使用ServiceStack,我只想要返回304未修改,如下所示:
我该如何防止输出其他标头?我已尝试使用HttpResult进行各种尝试,注册虚拟内容类型过滤器,但正如其名称所示,它只控制内容而不是标头,或者在此处列出的其他方法here。我还尝试使用IStreamWriter和IHasOptions实现自己的IHttpResult导出,但结果相同:ServiceStack添加了不需要的标头。
谢谢
更新
通过使用以下方法,我能够删除
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
, server
和date
。 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)
{
}
}
GlobalResponseHeaders
中或使用HttpResult
逐个服务添加的发射标头不属于ServiceStack。IIS / ASP.NET可能会选择添加自己的标头。 - mythzGlobalResponseHeaders = new Dictionary<string,string>()
吗?这将删除 ServiceStack 标头,但不会添加任何序列化器。我不知道如何删除 HttpListener 添加的Server
HTTP 标头。 - mythz