首页 > 解决方案 > 将角度 KeyValue 序列化为 ASP.NET KeyValuePair 的正确语法是什么?

问题描述

我试图序列化一个 KeyValue“数组”,以通过 http 上的 PUT 将其发送到我的 asp.net 网络服务器。

Angular 中的函数如下所示:

SortAboutUs(data : KeyValue<number,number>[]) {
    this.dataTransmitter.Put(this.apiUrl+'/api/aboutus/sort', data);    
}

如果我调试以下数据容器,那么它看起来像这样:

数据容器

我的 .net 核心 Webserver 的控制器中有以下内容

[HttpPut("[action]")]
public ActionResult<bool> Sort(IList<KeyValuePair<int, int>> dto)
{
    return Ok(_aboutUsService.Sort(dto));
}

但是,尝试通过 PUT 发送时出现以下错误:

The JSON value could not be converted to System.Collections.Generic.List`1[System.Collections.Generic.KeyValuePair`2[System.Int32,System.Int32]]. Path: $[0] | LineNumber: 0 | BytePositionInLine: 8.

奇怪的是,我已经在另一个旧版本的 .net core 中使用了相同的技术,并且一切似乎都可以正常工作。

我还注意到,自从 .net core 3.1 以来,C# 中的 KeyValue 更改为 KeyValuePair,但在旧版本的 .net core 中,它是 KeyValue。

这是否与我的相关错误有关?

以及如何从 Angular 序列化 KeyValue 以便我的 Web 服务器可以读取它?

标签: angular.net-corekeyvaluepairhttp-put

解决方案


你的对象应该是这样的: -

[
 {
   4: 0
 },
 {
   5: 1
 },
 {
   6: 2
 }
]

推荐阅读