2

Essentially, I want all of my responses returned in JSON by default, searched for an answer and stumbled upon this discussion: ServiceStack default format

What I tried:

  1. Setting DefaultContentType to JSON and disabling Feature.Html --> works for responses, but breaks SwaggerUI (error on page render)

  2. Only setting DefaultContentType to JSON --> doesn't break SwaggerUI, but making requests to my services from browser returns HTML (which makes sense because browsers usually the Accept header to receive html or xml, but I want to default to JSON)

That said, is there any way to only (and safely) enable Feature.Html for SwaggerUI? Maybe using PreRequestFilters?

natasha
  • 263
  • 1
  • 9

1 Answers1

2

The issue is removing the HTML Format essentially removes HTML ContentType from consideration, but I've changed it to preserve the Content Type if the Service returns a raw HTML string in this commit where the Swagger UI can return HTML pages even if the HTML Format is disabled.

This change is available from v5.4.1 that's now available on MyGet.

An alternative is to leave the HTML Format enabled but use a request filter to change the Content Type to JSON where it's HTML for all Requests you want to do this for, e.g:

PreRequestFilters.Add((req, res) => {
    if (req.ResponseContentType.Matches(MimeTypes.Html) && !req.PathInfo.StartsWith("/swagger-ui"))
        req.ResponseContentType = MimeTypes.Json;
});
mythz
  • 141,670
  • 29
  • 246
  • 390