KevinDockx / JsonPatch

JSON Patch (JsonPatchDocument) RFC 6902 implementation for .NET
MIT License
174 stars 28 forks source link

No MediaTypeFormatter is available #102

Open HamitEnes opened 4 years ago

HamitEnes commented 4 years ago

Hello,

I Have aspnet webapi project .Net Framework 4.6.1 and installed below nuget package.

'package id="Marvin.JsonPatch" version="2.1.1" targetFramework="net461" '

I have an ODataController and Patch method with HttpPatch attribute

public class SalesInvoicesController : Microsoft.AspNet.OData.ODataController { [System.Web.Http.HttpPatch] public IHttpActionResult Patch([FromODataUri] int key, [FromBody] JsonPatchDocument'SalesInvoice' salesInvoicePatch) { // get the expense from the repository SalesInvoice existingSalesInvoice = ....

            // apply the patch document 
            salesInvoicePatch.ApplyTo(existingSalesInvoice);
    }

}

using POSTMAN for example request either application/json or application/json-patch+json content-type request fail with this exception. I couldn't find any info in readme about that.

Exception thrown: 'System.Net.Http.UnsupportedMediaTypeException' in System.Net.Http.Formatting.dll No MediaTypeFormatter is available to read an object of type 'JsonPatchDocument`1' from content with media type 'application/json-patch+json'.

request body like that,

[ { "op": "replace", "path" : "/DocumentNo", "value" : "A-303030" } ]

response http 415 Unsupported Media Type

Additional Info : I added this part of code to my app start from one of closed issues

        config.Formatters.JsonFormatter.SupportedMediaTypes.Add(
            new MediaTypeHeaderValue("application/json-patch+json"));

How can I solve that? Thanks

Is there any additional setup for usage this nuget?