difference post structure in .net core 2.2 and 3.0

Viewed 79

in my project with .net core 2.2 i use postman to test APIs.
in postman
(1) set content-type: application/json
(2) body => raw & json and i send simple JavaScript object like this:

{
    pageSize : 10,
    pageNumber : 0,
    sortType : 'nn',
    keyword : 'ff',
    cityCode : 1,
    citySlug : 'f',
    priceRange : 'ff',
    starRange : 'fff',
    hotelFacilityRange : 'fffff',
    roomFacilityRange : 'fs',
    hotelGroup : 'dss',
    hotelType : 'sss',
    foodType : 'ss'
}

but yesterday i upgrade to .net core 3.0
and now i must send json string, and doesn't accept simple object.
what is wrong?
when i send simple object i get null in api, but when i convert to json "pageSize" : 10, "pageNumber": 0 ... it works!!!

1 Answers

but yesterday i upgrade to .net core 3.0 and now i must send json string, and doesn't accept simple object.

Indeed. I could reproduce the same issue as yours. But I don't think that's a bug because ASP.NET Core 3.0 now uses System.Text.Json as the default tool to serialize/deserialize json. See official docs.

However, according to json.org, the string should start with " instead of '. :

JSON string
(source: json.org)

In short, the way you did in old days is not a standard JSON representation. The reason why you can do that is JSON.NET does it for you automatically.


To enable the old behavior, you could follow Microsoft's advice here:

  1. Add a package reference to JSON.NET
  2. Add .AddNewtonsoftJson() for MVC:
 public void ConfigureServices(IServiceCollection services)
 {
     services.AddControllersWithViews().AddNewtonsoftJson();
 }
Related