Where did IMvcBuilder AddJsonOptions go in .Net Core 3.0?

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for Where did IMvcBuilder AddJsonOptions go in .Net Core 3.0?

📝 Where did IMvcBuilder AddJsonOptions go in .Net Core 3.0?

So, you just upgraded your ASP web API project from .Net Core 2.0 to 3.0 and encountered an error related to the AddJsonOptions method. Don't worry, you're not alone in this confusion! Let's dive into the issue and find a solution together. 😊

In the earlier version of .Net Core, you were probably using the following code to ensure the lower-casing of serialized JSON:

services.AddMvc()
        .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());

However, when you upgraded to .Net Core 3.0, you encountered the following error:

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

According to a Stack Overflow post, the AddJsonOptions extension method is/was provided by the Microsoft.AspNetCore.Mvc.Formatters.Json NuGet package.

Here comes the tricky part: even after installing or reinstalling the package, the method cannot be resolved. Moreover, when trying to add the using statement, IntelliSense only shows Microsoft.AspNetCore.Mvc.Formatters.Xml instead of Json, which can be quite confusing.

So, what's going on? 🤔

The reason for this confusion is that starting from .Net Core 3.0, the AddJsonOptions method has been deprecated. Microsoft has introduced a new configuration mechanism for JSON serialization options.

To solve this issue, you need to modify your code in the following way:

services.AddMvc()
        .AddJsonOptions(options =>
        {
            options.JsonSerializerOptions.PropertyNamingPolicy = null;
        });

By using the JsonSerializerOptions property, you can configure various serialization options, including naming policies, converters, and more.

Now you might be wondering, where is the documentation for this? Good question! The official Microsoft documentation for AddJsonOptions currently only goes up to .Net Core 2.2. However, you can find detailed information about the new configuration mechanism for JSON serialization options in the .Net Core 3.0 documentation.

Don't forget to update your code to reflect these changes, and you should be good to go with the lower-casing of serialized JSON in your .Net Core 3.0 project! 🚀

If you have any other questions or facing any further issues, feel free to ask in the comments section below. Happy coding! 💻💪


📣 Call-to-Action:

Did you find this blog post helpful in resolving your issue with AddJsonOptions in .Net Core 3.0? If yes, make sure to share it with others who might be facing the same problem. Let's spread the knowledge and help the developer community!

Also, don't hesitate to subscribe to our newsletter to receive more useful tips, guides, and updates directly to your inbox. Stay tuned for more exciting content! 👍💌

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my