ServiceStack pre-computes the requested content type for several factors (for example, Accept: header, QueryString, etc.), it stores this information in the httpReq.ResponseContentType property.
You can use this together with the IAppHost.ContentTypeFilters registry, which stores a collection of all serializers of registered content types in ServiceStack (i.e., built-in + user-defined) and do something like:
var dto = ...; var contentType = httpReq.ResponseContentType; var serializer = EndpointHost.AppHost .ContentTypeFilters.GetResponseSerializer(contentType); if (serializer == null) throw new Exception("Content-Type {0} does not exist".Fmt(contentType)); var serializationContext = new HttpRequestContext(httpReq, httpRes, dto); serializer(serializationContext, dto, httpRes); httpRes.EndServiceStackRequest();
Note: this simply serializes the response to the output stream; it does not execute any other request or response filters or other user hooks in accordance with the usual ServiceStack request.
source share