[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.


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

namespace API_IN_COMPARE
{
    public class Startup
    {
        private IConfiguration _config;
        public Startup(IConfiguration configuration)
        {
            _config = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews(options =>
            {
                options.InputFormatters.Insert(0, GetJsonPatchInputFormatter());
            });
            
            services.AddControllersWithViews().AddNewtonsoftJson();
            services.AddMvc();
            services.AddDbContextPool<InCompareContext>(options =>
            {
                options.UseSqlServer(_config.GetConnectionString("EmployeeDbConnection"));
            });
            services.AddScoped<ICategory, CategoryRepository>();

            services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

        }


        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }

        private static NewtonsoftJsonPatchInputFormatter GetJsonPatchInputFormatter()
        {
            var builder = new ServiceCollection()
                .AddLogging().AddMvc().AddNewtonsoftJson().Services.BuildServiceProvider();

            return builder
                .GetRequiredService<IOptions<MvcOptions>>()
                .Value
                .InputFormatters
                .OfType<NewtonsoftJsonPatchInputFormatter>()
                .First();
        }

    }
}

Summery

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.

Default image
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.

Leave a Reply