Where did IMvcBuilder AddJsonOptions go in .Net Core 3.0?
As part of ASP.NET Core 3.0, the team moved away from including Json.NET by default. You can read more about that in general in the announcement on breaking changes to Microsoft.AspNetCore.App.
Instead of Json.NET, ASP.NET Core 3.0 and .NET Core 3.0 include a different JSON API that focuses a bit more on performance. You can learn about that more in the announcement about “The future of JSON in .NET Core 3.0”.
The new templates for ASP.NET Core will no longer bundle with Json.NET but you can easily reconfigure the project to use it instead of the new JSON library. This is important for both compatibility with older projects and also because the new library is not supposed to be a full replacement, so you won't see the full feature set there.
In order to reconfigure your ASP.NET Core 3.0 project with Json.NET, you will need to add a NuGet reference to Microsoft.AspNetCore.Mvc.NewtonsoftJson
, which is the package that includes all the necessary bits. Then, in the Startup’s ConfigureServices
, you will need to configure MVC like this:
services.AddControllers()
.AddNewtonsoftJson();
This sets up MVC controllers and configures it to use Json.NET instead of that new API. Instead of controllers, you can also use a different MVC overload (e.g. for controllers with views, or Razor pages). That AddNewtonsoftJson
method has an overload that allows you to configure the Json.NET options like you were used to with AddJsonOptions
in ASP.NET Core 2.x.
services.AddControllers()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
How do I solve AddJsonOptions does not contain definition of SerializerSettings - .NET
Solved.
services.AddMvc().AddNewtonsoftJson(o =>
{
o.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
Hope this helps.
NuGet: Microsoft.AspNetCore.Mvc.NewtonsoftJson
Migrate from /net core 2.1 to .net core 3.1
i am getting issue with services.AddMvc(options => {
options.Filters.Add(new AuthorizeFilter("default"));
}).AddJsonOptions(x => x.SerializerSettings.ReferenceLoopHandling =
Newtonsoft.Json.ReferenceLoopHandling.Serialize) 'JsonOptions' does
not contain a definition for 'SerializerSettings'
For asp.net core 3.0+,you need to install the package Microsoft.AspNetCore.Mvc.NewtonsoftJson for your version firstly,then replace
services.AddMvc()
.AddJsonOptions(
options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize);
with
services.AddControllersWithViews()
.AddNewtonsoftJson(options =>
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize);
Refer to https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.1&tabs=visual-studio#use-newtonsoftjson-in-an-aspnet-core-30-mvc-project
I can not use the 'services.AddControllers().AddNewtonsoftJson()'
You need also to install Microsoft.AspNetCore.Mvc.NewtonsoftJson which contains corresponding extension methods, as written in the docs .
.net core 3 not having ReferenceLoopHandling in AddJsonOptions
As part of the work to improve the ASP.NET Core shared framework, Json.NET has been removed from the ASP.NET Core shared framework. Your app may require this reference if it uses Newtonsoft.Json-specific feature such as JsonPatch or converters or if it formats Newtonsoft.Json-specific types.
To use Json.NET in an ASP.NET Core 3.0 project:
Add a package reference to Microsoft.AspNetCore.Mvc.NewtonsoftJson.
Update Startup.ConfigureServices
to call AddNewtonsoftJson
.
services.AddMvc()
.AddNewtonsoftJson();
This sets up MVC and configures it to use Json.NET instead of that new API. And that AddNewtonsoftJson method has an overload that allows you to configure the Json.NET options like you were used to with AddJsonOptions in ASP.NET Core 2.x.
services.AddMvc()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings = new JsonSerializerSettings() { … };
});
Reference:
https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-2.2&tabs=visual-studio#jsonnet-support
https://stackoverflow.com/a/55666898/10201850
JsonOutputFormatter in ASP.NET Core 3.0
I personally use Json.NET
- Simply add a package reference to Microsoft.AspNetCore.Mvc.NewtonsoftJson.
- Update Startup.ConfigureServices to call AddNewtonsoftJson.
services.AddMvc().AddNewtonsoftJson();
Json.NET settings can be set in the call to AddNewtonsoftJson
:
services.AddMvc()
.AddNewtonsoftJson(options =>
options.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver());
I am using the default options with compatibility mode
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
.AddNewtonsoftJson(options => { options.SerializerSettings.ContractResolver =
new DefaultContractResolver(); });
Reference
Migrate from ASP.Net 2.2 to 3.0
AddJsonOptions for MvcJsonOptions in Asp.Net Core 2.2
As mentioned here MvcJsonOptions
is moving to Microsoft.AspNetCore.Mvc
in Asp.net core 2.2 as it's stated at this documentation too and already started to move there for asp.net core 2.1, and also the documentation for Asp.net core 2.2 is not updated yet and that is why we don't see it there.
Related Topics
How to Get the Ip Address of the Server on Which My C# Application Is Running On
How to Convert a Datatable into a Generic List
Why Does C# Forbid Generic Attribute Types
How to Delete an Element from an Array in C#
Calculate Date from Week Number
Mocking Extension Methods With Moq
How to Clone a Range of Array Elements to a New Array
How to Write a Simple Html.Dropdownlistfor()
How to Start a Process as Administrator Mode in C#
How to Make a Window Always Stay on Top in .Net
How to Build Splash Screen in Windows Forms Application
Translucent Circular Control With Text
Why Do We Need Boxing and Unboxing in C#
Use Linq to Get Items in One List≪≫, That Are Not in Another List≪≫
Most Efficient Way to Randomly "Sort" (Shuffle) a List of Integers in C#