asp.net - Reject invalid content types with HTTP 406 not acceptable -


I am working on a project where we want to restrict the number of content types and throw appropriate errors when invalid Contains content type.

Currently we remove XML formulas from the list of allowed formats, except JSON as an option. The acceptance or effect of JSON is the default for all unknown content types in the content-type header.

However, I would like to modify the content conversation so that the request for XML, or, or anyone, will throw non-JSON content type HTTP 406 unacceptable error .

What is the best practice for this? Should I install a global filter? Should I make my own contentnotator and replace the default in global configuration?

Title about unsupported media types Accept : Setting the Excluded Type DefaultContentNegotiator sends the service back to the unknown media for 406 responses instead of the default for JSON type:

  Default Content Planner Default Negative = New DefaultContentNegotiator (Type MatchOnly: True); Config.SServices.Replace (IContentNegotiator, Default Negative);  

Regarding posting unknown content media type : It depends on the version you use. Web API 2 and later, you should see any 415 unsupported media type response when a client posts data with content type that does not support the service.


Comments