"415 Unsupported Media Type" For Content-Type "application/csp-report" In ASP.NET Core
Answer :
The following example shows how to add support to the SystemTextJsonInputFormatter
for handling additional media-types:
services.AddControllers(options => { var jsonInputFormatter = options.InputFormatters .OfType<SystemTextJsonInputFormatter>() .Single(); jsonInputFormatter.SupportedMediaTypes.Add("application/csp-report"); });
This is a two-step process:
- Interrogate the configured list of input-formatters to find the
SystemTextJsonInputFormatter
. - Add
application/csp-report
to its existing list of supported media-types (application/json
,text/json
, andapplication/*+json
).
If you're using Json.NET instead of System.Text.Json
, the approach is similar:
services.AddControllers(options => { var jsonInputFormatter = options.InputFormatters .OfType<NewtonsoftJsonInputFormatter>() .First(); jsonInputFormatter.SupportedMediaTypes.Add("application/csp-report"); })
There are two small differences:
- The type is
NewtonsoftJsonInputFormatter
instead ofSystemTextJsonInputFormatter
. - There are two instances of this type in the collection, so we target the first (see this answer for the specifics).
See Input Formatters in the ASP.NET Core docs to learn more about those.
Comments
Post a Comment