[Solved] The JSON value could not be converted to Microsoft.AspNetCore.JsonPatch.JsonPatchDocument in asp.net core 3.0 Http patch

Problem

I have created an HTTP patch method by using JsonPatchDocument but I was unable to send the request to the server, I’m getting the below error.

The JSON value could not be converted to Microsoft.AspNetCore.JsonPatch.JsonPatchDocument`1[API_IN_COMPARE.Models.Categorydto]. Path: $ | LineNumber: 0 | BytePositionInLine: 1.

Solution

I followed the below approaches to solve this issue, I will try to explain how I solved the issue in my asp.net Core web API project

Step 1: install Nuget package

I highly recommend you install Microsoft.AspNetCore.MVC.NewtonsoftJson 3.1.10v package to the ap.net core web API project. If you are working with .net core 3.1v. We will face some below issues when we install the latest NewtonsoftJson package to the application.

So to avoid such type of issues, we can downgrade the package version to the below image recommended version to install newtonsoftJson package to our project.

Step 2: Configure the services

Add the two AddControllersWithViews() and AddNewtonsoftJson(); services to your configure service method of the startup.cs class file. The preceding code requires the Microsoft.AspNetCore.Mvc.NewtonsoftJson package and the following using statements:

using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Formatters; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; using System.Linq;

And, add NewtonsoftJsonPatchInputFormatter private method in starup.cs file.

Please use the following code changes as your reference, Only the highlighted lines should be copied to your project.

Summary

In short, in asp.net core there was an issue while installing the packages for the HTTP patch method, I solved the error by follow-up the above steps, I hope you find them helpful.

Shivaraju M

Over 3+ years experience in IT industry, good knowledge in . Net-based Web applications, windows applications, web services, and SPAs. I have played multiple roles related to technical delivery in multiple domains while working on over 10+ technologies. I am currently focused on continuous improvement and agile project management.

Post a Comment

Previous Post Next Post