简体   繁体   English

模型绑定从 2.2 更改为 3.0 Asp.NetCore

[英]Has model binding changed from 2.2 to 3.0 Asp.NetCore

I am attempting to hit an endpoint on a 3.0 api.我正在尝试访问 3.0 api 上的端点。 I am passing in all parameters the same way the existing application does using a dictionary of type string, string.我以与现有应用程序相同的方式传递所有参数,使用字符串类型的字典。 But now i am getting a 400 bad request and it is because of the property UserId is an int.但是现在我收到了一个 400 错误的请求,这是因为属性 UserId 是一个 int。

public class ConfirmCreatedRequest
{
    public int UserId { get; set; }
    public string Password { get; set; }
    public string MemorableWord { get; set; }
}

Here is an example of the json using the original dictionary and changing the dictionary to string, object.下面是使用原始字典并将字典更改为字符串、对象的 json 示例。

"{\"UserId\":\"90744\",\"Password\":\"\",\"MemorableWord\":\"\"}"
"{\"UserId\":90750,\"Password\":\"\",\"MemorableWord\":\"\"}"

The thing is i do not want to do this as it not type safe.问题是我不想这样做,因为它不是类型安全的。

I want to know what has changed in 3.0 and has it been fixed in 3.0.1我想知道 3.0 中发生了什么变化以及它是否已在 3.0.1 中修复

The default serializer has changed from Newtonsoft.Json to System.Text.Json , so some of the serialization behaviours will change.默认序列化Newtonsoft.Json已从Newtonsoft.Json更改为System.Text.Json ,因此某些序列化行为将发生变化。

If you want it to behave like it did in 2.x you can make Newtonsoft.Json the default with:如果您希望它的行为与2.x一样,您可以使用以下命令将Newtonsoft.Json设为默认值:

services.AddMvc()
    .AddNewtonsoftJson();

See the docs here: https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.0&tabs=visual-studio#jsonnet-support请参阅此处的文档: https : //docs.microsoft.com/en-us/aspnet/core/migration/22-to-30? view = aspnetcore-3.0 & tabs = visual-studio#jsonnet- support

If you want to make your current JSON and type serialize/deserialize as they have done with the new System.Text.Json , you can read up here: https://github.com/dotnet/corefx/issues/39473如果您想制作当前的 JSON 并像他们对新System.Text.Json所做的那样输入序列化/反序列化,您可以在这里阅读: https : //github.com/dotnet/corefx/issues/39473

This issue was raised by Stackoverflows Nick Craver, in it there are custom converters that give you what you want, and an indication that there is a plan to support "looser" deserialization in 5.0 .这个问题是由 Stackoverflows Nick Craver 提出的,其中有自定义转换器可以为您提供所需的内容,并表明有计划支持5.0 “更宽松”的反序列化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM