Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model binding stopped working after migrating from .NET Core 2.2 to 3.0-preview-9

I have an Angular front-end app and an ASP.NET Core back-end app. Everything was fine until I decided to migrate from ASP.NET Core 2.2 to 3.0-preview-9.

For example, I have a DTO class:

public class DutyRateDto
{
    public string Code { get; set; }
    public string Name { get; set; }
    public decimal Rate { get; set; }
}

And an example JSON request:

{
    "name":"F",
    "code":"F",
    "rate":"123"
}

Before migration, this was a valid request because 123 was parsed as a decimal. But now, after migration, I am getting an HTTP 400 error with this body:

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "|3293cead-4a35656a3ae5e95b.",
    "errors": {
        "$.rate": [
            "The JSON value could not be converted to System.Decimal. Path: $.rate | LineNumber: 0 | BytePositionInLine: 35."
        ]
    }
}

In addition, it doesn't hit the first line of my method—it is thrown before, probably during model binding.

If I send the following, however:

{
    "name":"F",
    "code":"F",
    "rate":123
}

Everything works fine. But this means that I need to change every request I have written so far. The same thing is true with all other data types (int, bool, …).

Does anybody know how I can avoid this and make it work without changing my requests?

like image 426
Dusan Radovanovic Avatar asked Sep 10 '19 14:09

Dusan Radovanovic


1 Answers

ASP.NET Core 3 uses System.Text.Json instead of Newtonsoft.Json (aka JSON.NET) for handling JSON. JSON.NET supports parsing from a string into a decimal, but System.Text.Json does not. The easiest thing to do at this stage is to switch back to using JSON.NET, as described in the docs:

  • Add a package reference to Microsoft.AspNetCore.Mvc.NewtonsoftJson.
  • Update Startup.ConfigureServices to call AddNewtonsoftJson.

    services.AddMvc()
          .AddNewtonsoftJson();
    

It would be more correct to pass the decimal as a JSON number, but it's clear that's not going to be an option for you.

like image 151
Kirk Larkin Avatar answered Oct 16 '22 18:10

Kirk Larkin