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.



