How to fix 'IMvcBuilder' does not contain a definition for 'AddNewtonsoftJson'

By FoxLearn 1/11/2025 2:59:37 AM   1.62K
The error message you are seeing, "IMvcBuilder does not contain a definition for AddNewtonsoftJson", typically occurs when you're trying to add Newtonsoft.Json support to an ASP.NET Core MVC application, but the necessary package or using directive is missing.

This method is typically used to configure JSON serialization settings in ASP.NET Core applications using Newtonsoft.Json instead of the built-in System.Text.Json.

Error CS1061 'IMvcBuilder' does not contain a definition for 'AddNewtonsoftJson' and no accessible extension method 'AddNewtonsoftJson' accepting a first argument of type 'IMvcBuilder' could be found (are you missing a using directive or an assembly reference?)

Install the Newtonsoft.Json NuGet package

To resolve the error 'addnewtonsoftjson not found', ensure that your project has the Microsoft.AspNetCore.Mvc.NewtonsoftJson package installed. This package provides the extension method AddNewtonsoftJson that you are trying to use.

To install the package Microsoft.AspNetCore.Mvc.NewtonsoftJson, you can right click on your project, then select Manage Nuget Packages => Install

Microsoft.AspNetCore.Mvc.NewtonsoftJson

You can also install it by running the following command in the Package Manager Console:

dotnet add package Microsoft.AspNetCore.Mvc.NewtonsoftJson

Add the correct using directive

If it's already installed, make sure you have imported the namespace Microsoft.Extensions.DependencyInjection where the extension method AddNewtonsoftJson is defined.

using Microsoft.Extensions.DependencyInjection;

In your Startup.cs or Program.cs file, in the ConfigureServices method, use the AddNewtonsoftJson extension method correctly:

// addnewtonsoftjson a not imvcbuilder
services.AddControllers().AddNewtonsoftJson();

For new ASP.NET Core version (for .NET 6 or later)

// addnewtonsoftjson .net 6
// addnewtonsoftjson .net 8 or .net 9
builder.Services.AddControllers().AddNewtonsoftJson();

Configure Newtonsoft.Json in Program.cs or Startup.cs file

By default, ASP.NET Core uses System.Text.Json for JSON serialization and deserialization.

To use Newtonsoft.Json, you need to configure it in the Program.cs or Startup.cs file.

In Program.cs (for .NET 6 or later):

Add Newtonsoft.Json as the JSON serializer for the application:

// imvcbuilder addnewtonsoftjson .net 8 or .net 9
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container (fix addnewtonsoftjson not found .net 8)
builder.Services.AddControllers().AddNewtonsoftJson(options =>
    {
        // Configure additional settings if needed, for example:
        options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    });

var app = builder.Build();

// Configure the HTTP request pipeline.
app.UseRouting();

app.MapControllers();

app.Run();

Adding NewtonsoftJson for old  ASP.NET Core version.

// addnewtonsoftjson not imvcbuilder found
public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers()
                .AddNewtonsoftJson(options =>
                {
                    // your settings...
                });
    }
}

After adding the package and configuring the JSON serialization, the AddNewtonsoftJson method should be recognized by the IMvcBuilder.

Use Newtonsoft.Json in your controllers

Now, you can use Newtonsoft.Json in your controllers for JSON serialization and deserialization.

using Microsoft.AspNetCore.Mvc;

namespace YourApp.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class SampleController : ControllerBase
    {
        [HttpGet]
        public IActionResult Get()
        {
            var data = new { Name = "John", Age = 30 };
            return Ok(data);
        }
    }
}

Once you've configured everything, run your application, and ASP.NET Core will use Newtonsoft.Json for JSON serialization and deserialization in API responses and requests.