简体   繁体   English

Why different behavior between ASP.NET Web API 2 & ASP.NET Core 3.1 Web API?

[英]Why different behavior between ASP.NET Web API 2 & ASP.NET Core 3.1 Web API?

I am posting below data to 2 different api endpoints, one we have in an ASP.NET Web API 2 application and while other I have in an ASP.NET Core 3.1 Web API application. I am posting below data to 2 different api endpoints, one we have in an ASP.NET Web API 2 application and while other I have in an ASP.NET Core 3.1 Web API application.

var data = new Data
            {
                Key = "k1",
                Value = 80
            };

IClient httpClient = new FluentClient("http://localhost:46551/");
var x = httpClient.PostAsync("weatherforecast", data).GetAwaiter().GetResult();

For both Web API applications, my model class is below where Value is object type and this is my hard requirement, I can't go with actual type. For both Web API applications, my model class is below where Value is object type and this is my hard requirement, I can't go with actual type.

public class Data
{
    public string Key { get; set; }
    public object Value { get; set; }
}

When I am posting data to ASP.NET Web API 2 (with.Net 4.6.1), the VS debugger shows only value correctly without any other payload:当我将数据发布到 ASP.NET Web API 2(使用.Net 4.6.1)时,VS 调试器仅正确显示值,而没有任何其他有效负载:

在此处输入图像描述

But for ASP.NET Core 3.1 Web API, the VS debugger shows like this, totally different representation along with ValueKind :但是对于 ASP.NET Core 3.1 Web API,VS 调试器显示如下,与ValueKind完全不同的表示:

在此处输入图像描述

I have a requirement to write data into Json format with below expected output,我需要将data写入 Json 格式,低于预期的 output,

{ "Key":"k1", "Value":80 }

While I am trying to serialize data for the ASP.NET Core 3.1 Web API, I get this output:当我尝试序列化 ASP.NET Core 3.1 Web API 的data时,我得到了这个 Z78E6221F6398FD1:

var ser = JsonConvert.SerializeObject(data);

{ "Key":"k1", "Value": {"ValueKind":4} }

For the ASP.NET Web API 2 application, though I am getting expected output.对于 ASP.NET Web API 2 应用程序,虽然我得到了预期 Z78E6221F6393D1356D681CEDB3

Question, how to get this output { "Key":"k1", "Value":80 } from the ASP.NET Core 3.1 application and why there is a different behavior from both applications?问题,如何从 ASP.NET Core 3.1 应用程序中获取此 output { "Key":"k1", "Value":80 }以及为什么两个应用程序的行为不同?

Thanks,谢谢,

Were you using Newtonsoft previously?您以前使用过 Newtonsoft 吗? The JSON formatter now included with netcore 3x is default unless you explicitly include another parser such as NewtonSoft.现在包含在 netcore 3x 中的 JSON 格式化程序是默认的,除非您明确包含另一个解析器,例如 NewtonSoft。 The behavior of the new/default parser has a number of differences from Newtonsoft but the one that has caused my teams the most grief when migrating is that the System.Text.Json parser does NOT make assumptions when identifying JSON.新/默认解析器的行为与 Newtonsoft 有许多不同,但在迁移时让我的团队最痛苦的是 System.Text.Json 解析器在识别 JSON 时没有做出假设。 This was most commonly where sometimes a field may be a sometime take a complex JSON object and also contain primitive types in cases such as a boolean or integer. This was most commonly where sometimes a field may be a sometime take a complex JSON object and also contain primitive types in cases such as a boolean or integer. A concrete example for me was a settings concept where it was basically a key/value pair, where key was name of the setting such as GridSettings which was a blob and stored in Postgres as JSON, however another setting such as showHidden was a simple true or false.对我来说,一个具体的例子是一个设置概念,它基本上是一个键/值对,其中键是设置的名称,例如 GridSettings,它是一个 blob,并在 Postgres 中存储为 JSON,但是另一个设置,如 showHidden 是一个简单的 true或假的。 Newtonsoft would still coerce that boolean into JSON since the backing model and entity model declared it as such where as System.Text.Json says nope, that's just a boolean unless you tell me otherwise. Newtonsoft would still coerce that boolean into JSON since the backing model and entity model declared it as such where as System.Text.Json says nope, that's just a boolean unless you tell me otherwise.

Try injecting Newtonsoft as your parser in your IoC/Startup and see if that changes the behavior.尝试在 IoC/Startup 中注入 Newtonsoft 作为解析器,看看是否会改变行为。

some details around differences in new parser: https://docs.microsoft.com/en-us/dotnet/core/compatibility/aspnetcore#authentication-newtonsoftjson-types-replaced有关新解析器差异的一些细节: https://docs.microsoft.com/en-us/dotnet/core/compatibility/aspnetcore#authentication-newtonsoftjson-types-replaced

We also had to make adjustments to our formatters in certain scenarios:在某些情况下,我们还必须对格式化程序进行调整:

custom formatter: https://code-maze.com/content-negotiation-dotnet-core/自定义格式化程序: https://code-maze.com/content-negotiation-dotnet-core/

All in all I do think the parser which makes no assumptions means well and probably is a good thing.总而言之,我确实认为不做任何假设的解析器意味着好,并且可能是一件好事。 However in my case we simply were not ready to make all the corrections needed to counteract this change.但是在我的情况下,我们根本没有准备好进行所有需要的更正来抵消这种变化。 Thus for now, we're sticking with good old Newtonsoft.因此,就目前而言,我们坚持使用旧的 Newtonsoft。

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

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