无法从端点参数读取数据,只能读取 TokenType 和 ValueKind

Can't read data from endpoint parameter, only TokenType and ValueKind

我正在尝试设置 webhook 并在我的端点中接收负载。有效载荷通过 http 发送(如 json)到我的端点,我在其中通过端点操作中的参数拦截有效载荷请求体。 仅当我将负载类型设置为 'object' 时才会触发该操作。我尝试使用 JSON-payload 创建 1 对 1 映射的模型;我什至确保它区分大小写。

当我使用我的自定义 class(具有相应的属性)作为 [FromBody] 动作参数时,它不会被触发。当我使用 'object' 参数类型时,它确实会被触发,但我无法访问任何数据。 我还尝试使用 NewtonSoft.JSON 将对象转换为 JObject,但这仅 returns ValueKind 属性(在检查传入对象时也可见)。

如何将负载中的数据映射到我的自定义对象?或者为什么当我使用我的自定义模型而不是对象参数时端点不触发?

使用对象作为参数时的端点

    [AllowAnonymous]
    [HttpPost("{userId}/follows")]
    public void HandleReceivedFollow([FromRoute(Name = "userId")] string userId, [FromBody] object response)
    {
        Console.WriteLine(response);
        //PROCESS DATA AND SEND OVER WSS
    }

使用我的自定义class作为参数时的端点

    [AllowAnonymous]
    [HttpPost("{userId}/follows")]
    public void HandleReceivedFollow([FromRoute(Name = "userId")] string userId, [FromBody] TwitchResponse<UserFollowResponse> response)
    {
        Console.WriteLine(response);
        //PROCESS DATA AND SEND OVER WSS
    }

有效负载以以下形式从 webhook 发送:

    {
        "data": 
        {
            "field1": 0,
            "field2": 1
        }
    }

我的自定义 class 看起来像:

TwitchResponse

public class TwitchResponse<T> : ITwitchResponse<T>
{

    public T data { get; set; }

    public TwitchResponse() { }

    public TwitchResponse(T data)
    {
        this.data = data;
    }

}

用户关注响应

    public class UserFollowResponse
{

    public string from_id { get; set; }
    public string from_name { get; set; }
    public string to_id { get; set; }
    public string to_name { get; set; }
    public string followed_at { get; set; }

    public UserFollowResponse() { }

    public UserFollowResponse(string from_id, string from_name, string to_id, string to_name, string followed_at)
    {
        this.from_id = from_id;
        this.from_name = from_name;
        this.to_id = to_id;
        this.to_name = to_name;
        this.followed_at = followed_at;
    }

}

当我使用对象参数时,我在端点接收到的负载:

ValueKind = Object : "{"data":[{"followed_at":"2020-07-07T11:38:16Z","from_id":"70700448","from_name":"HetDiamondSword","to_id":"166294598","to_name":"RamonPeekFifa"}]}"

我看了这个JSON:

{
   "data":[
      {
         "followed_at":"2020-07-07T11:38:16Z",
         "from_id":"70700448",
         "from_name":"HetDiamondSword",
         "to_id":"166294598",
         "to_name":"RamonPeekFifa"
      }
   ]
}

看起来“数据”元素是一个数组。

你能否修改你的动作,使其看起来像这样:

[AllowAnonymous]
    [HttpPost("{userId}/follows")]
    public void HandleReceivedFollow([FromRoute(Name = "userId")] string userId, [FromBody] TwitchResponse<IEnumerable<UserFollowResponse>> response)
    {
        Console.WriteLine(response);
        //PROCESS DATA AND SEND OVER WSS
    }

如果有任何变化,请告诉我们?