2012-08-07 7 views
6

Usando ServiceStack, sólo quiero volver 304 Not Modified como tal:Prevenir cabeceras no deseados cuando regresan 304 sin modificar con ServiceStack

HTTP/1.1 304 Not Modified 

Pero ServiceStack añade muchas otras no deseado (volviendo HttpResult con 304 códigos) encabezados como tal:

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 

¿Cómo puedo evitar que salgan los otros encabezados? He intentado varios enfoques con HttpResult, registrando un filtro de tipo de contenido ficticio, pero como su nombre implica solo controla el contenido, no los encabezados, u otros listados here. También intenté implementar mi propio derivado IHttpResult con IStreamWriter y IHasOptions con los mismos resultados: ServiceStack agrega encabezados no deseados.

Gracias

Actualizar

fue capaz de eliminar content-type mediante el uso de lo siguiente, pero algunas cabeceras todavía están presentes es decir content-length, server, y 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) 
    { 
    } 
} 

Respuesta

8

Las únicas cabeceras emitidos por ServiceStack son las registradas en el EndpointHostConfig.GlobalResponseHeaders.

Retirar ellos si no quiere que ellos emiten, por ejemplo:

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

Usted puede añadirlos en una base ad hoc, usando un HttpResult, por ejemplo:

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

Ambas opciones se explican en más detalle: servicestack REST API and CORS

+0

Gracias por la respuesta, pero esto solo elimina los CORS, no los demás. Todavía tengo HTTP/1.1 304 No modificado Content-Length: 0 Content-Type: application/json Caduca: Jue, 16 de agosto de 2012 16:20:25 GMT Servidor: Microsoft-HTTPAPI/2.0 Fecha: Jue, 16 de agosto de 2012 16:20:24 GMT –

+0

Los encabezados emitidos que no están en 'GlobalResponseHeaders' o que se agregan a usted por servicio con' HttpResult' no son de ServiceStack. IIS/ASP.NET puede elegir agregar sus propios encabezados. – mythz

+0

No usa IIS/ASP.Net, solo prueba de aplicación de consola. Parece añadido desde el serializador JSON y otros. ¿Cómo puedo controlar esto? –

0

en realidad, usted puede simplemente hacer algo como esto en su API

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

Que no devolvería ContentType, ContentLength, etc.

Cuestiones relacionadas