【问题标题】:The repeated fields in a protobuf message is empty in a POST api in ASP.NET Core在 ASP.NET Core 的 POST api 中,protobuf 消息中的重复字段为空
【发布时间】:2022-10-24 10:19:47
【问题描述】:

正如标题所说,当我尝试从反应应用程序向 ASP.NET Core 发布消息(由 protobuf 消息生成)时,后端无法接收重复的字段(空)。

以下是来自浏览器的 post 操作的有效负载:

{ "strs": [ "test1", "test2" ] }

enter image description here

但是在 ASP.NET Core 的 POST API 中,body 只是空的:

enter image description here

protobuf 消息非常简单:

message TestArray {
    repeated string strs = 1;
}

任何建议将被认真考虑。先感谢您。

C# 中生成的 TestArray 类的一部分:

using pb = global::Google.Protobuf;
using pbc = global::Google.Protobuf.Collections;
using pbr = global::Google.Protobuf.Reflection;

// ...

public sealed partial class TestArray : pb::IMessage<TestArray>

// ...

private readonly pbc::RepeatedField<string> strs_ = new pbc::RepeatedField<string>();

// ...

    public pbc::RepeatedField<string> Strs {
      get { return strs_; }
    }

【问题讨论】:

  • 你能在你的 api 中显示 TestArray 的详细信息吗?
  • [HttpPost("testarray")] public void TestArray(TestArray tests) { TestArray temp = tests; }
  • 抱歉,我不知道如何在评论中插入代码。

标签: asp.net-core protocol-buffers repeat


【解决方案1】:

我遵循了document 并创建了如下模型:

public class TestArray
    {        
        public RepeatedField<string> Strs { get; set; }
    }

用邮递员测试:

好像绑定成功了,所以我问你TestArray类的细节,看看有什么问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-19
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-09
    相关资源
    最近更新 更多