1
votes

Using ServiceStack 3.9.2x.

Out of the box (and as seen on the metadata page) service stack comes with built-in support for a bunch of content types - xml, json, jsv, etc. What is the best way to tell ServiceStack to limit the set of supported content. For example my service only knows how to speak JSON and I don't want ServiceStack to honor requests that sport "Content-type: application/xml" or "Accept: application/xml" headers. In said case I would like ServiceStack to respond with a 406 (Not Acceptable) response, ideally including in the body the set of supported content (per HTTP 1.1 spec).

Also how does ServiceStack decide what the default type of the content is for requests that do not sport an Accept or Content-Type header (I think I am seeing it render HTML now)? Is there a way to tell ServiceStack to assume a specific content type in these cases?

1

1 Answers

2
votes

See this answer to find out how to set the default content type in ServiceStack: ServiceStack default format

You can use a Request Filter to detect the requested content type with:

httpReq.ResponseContentType

In your global request filter you can choose to allow it (do nothing) or write directly to the response, e.g. 406 with list of supported content as you wish.

ServiceStack order of operations

The Implementation architecture diagram shows a visual cue of the order of operations that happens in ServiceStack. Where:

  1. EndointHostConfig.RawHttpHandlers are executed before anything else, i.e. returning any ASP.NET IHttpHandler by-passes ServiceStack completely.
  2. The IAppHost.PreRequestFilters gets executed before the Request DTO is deserialized
  3. Request Filter Attributes with Priority < 0 gets executed
  4. Then any Global Request Filters get executed
  5. Followed by Request Filter Attributes with Priority >= 0
  6. Action Request Filters (New API only)
  7. Then your Service is executed
  8. Action Response Filters (New API only)
  9. Followed by Response Filter Attributes with Priority < 0
  10. Then Global Response Filters
  11. Followed by Response Filter Attributes with Priority >= 0

Any time you close the Response in any of your filters, i.e. httpRes.Close() the processing of the response is short-circuited and no further processing is done on that request.