OData update controller throwing 415 Unsupported m

2019-06-06 01:48发布

问题:

I have an OData controller with 'Put' and 'Post' actions. When I post Content-Type:application/json, I get an HttpResponseException with '415: unsupported media type'. The only Content-Type that the controller won't throw an exception on is the application/x-www-urlformencoded. But that is not the content type that I want to accept.

Why does the odata controller not accept application/json? I don't see anything in the configuration of the controller that restricts the accepted content types.

I remember the same code working in the past. Soemthing seems to have regressed it. Where should I look?

回答1:

Feels lik the type that you expect in the body (i.e declared in your action as a parameter) is not part of the EDM model of your service.

For example,

HttpResponseMessage Post(Customer customer)

If customer is not part of your EDM model, web API would throw a 415. This happens as ODataControllers have only ODataMediaTypeFormatters and ODataMediaTypeFormatter supports only reading types that are declared in the model.