Reputation: 9508
My company uses a standard format for healthcheck responses in internal apis etc. We either return the status with a content type of application/status+json
on success or application/problem+json
if we have an issue (part of this proposed spec).
But if i set the content type to either of these my response becomes an emptu 406
response.
So, how can I tell the JsonOutputFormatter that it can add these Json header types to it's SupportedMediaTypes
collection?
I would expect I could do something like:
services.AddMvc().AddJsonOptions(jsonOptions => {
jsonOptions.SerializerSettings.SupportedMediaTypes.Add("application/problem+json");
});
But of course I can't find a way to do that.
Upvotes: 0
Views: 232
Reputation: 9508
Okay, so here is a way to do it. I found the OutputFormatters
collection and was able to pull out the JsonOutputFormatter. From there you can add a supported media type:
services.AddMvc(mvcOptions => {
//TODO: make extension method
var jFormatter = mvcOptions.OutputFormatters.FirstOrDefault(f => f.GetType() == typeof(JsonOutputFormatter)) as JsonOutputFormatter;
jFormatter?.SupportedMediaTypes.Add("application/problem+json");
jFormatter?.SupportedMediaTypes.Add("application/status+json");
});
Or, as an extension method:
public static IMvcBuilder AddStatusJsonSupport(this IMvcBuilder builder) {
builder.AddMvcOptions(options => {
var jFormatter = options.OutputFormatters.FirstOrDefault(f => f.GetType() == typeof(JsonOutputFormatter)) as JsonOutputFormatter;
jFormatter?.SupportedMediaTypes.Add("application/problem+json");
jFormatter?.SupportedMediaTypes.Add("application/status+json");
});
return builder;
}
called like so:
services.AddMvc().AddStatusJsonSupport();
Upvotes: 1