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

别等时光非礼了梦想. 提交于 2020-07-20 08:49:32

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/57873403/model-binding-stopped-working-after-migrating-from-net-core-2-2-to-3-0-preview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!