PUT将角度KeyValue序列化为ASP.NET KeyValuePair的正确语法是什么?

我试图序列化KeyValue“ Array”,以通过HTTP通过PUT将其发送到我的asp.net网络服务器。

Angular中的函数如下:

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

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

PUT将角度KeyValue序列化为ASP.NET KeyValuePair的正确语法是什么?

.net核心Web服务器的控制器中具有以下内容

[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核心的另一个旧版本中使用了相同的技术,并且似乎一切正常。

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

这与我的相关错误有关吗?

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

iCMS 回答:PUT将角度KeyValue序列化为ASP.NET KeyValuePair的正确语法是什么?

您的对象应该像:-

    self.image.image = UIImage(named: "horse_marked_100x100")
    self.imageScroller.maximumZoomScale = 10
    self.imageScroller.minimumZoomScale = 1
    self.imageScroller.clipsToBounds = true
    self.imageScroller.delegate = self
    self.imageScroller.isScrollEnabled = false
    self.imageScroller.scrollRectToVisible(CGRect(x: 100,y: 100,width: 100,height: 100),animated: false)
    self.imageScroller.zoomScale = 4
,

我能够通过创建自己的KeyValue类来解决此问题:

public class KeyValue<K,V>
{
    public K Key { get; set; }
    public V Value { get; set; }
}

在此之后,我的Controller方法现在看起来像这样:

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

由于采用了这种方法,控制器能够通过PUT接收从Angular发送到我的Web服务器的键值数组... enter image description here

本文链接:https://www.f2er.com/2104771.html

大家都在问