angular - 将角度 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 服务器可以读取它?
解决方案
你的对象应该是这样的: -
[
{
4: 0
},
{
5: 1
},
{
6: 2
}
]
推荐阅读
- html - Django ImageField 模板标签不显示
- python - 如何在 Python 中使用 networkx 计算加权传递性
- java - 如何在 Android 中打开 Kotlin 类 uisng Java 类?
- c# - Asp.net core 继续使用过期的证书
- laravel - 在 Livewire 上渲染整页组件之前检查资源是否存在
- c# - 如何检查字符串是否包含在字符串数组中?
- python - 如何用pygame制作蛇墙传送
- sql - Microsoft SMSS - 在 t2.primary key = t1.foreign_key 上连接表
- windows - 如何使用 Emscripten 将 Qhull 构建到 Javascript 中?
- python - 缺少数据处理的 While 循环