【问题标题】:I want to pass very long string as parameter in Asp .net core web api POST method?我想在 Asp .net core web api POST 方法中传递很长的字符串作为参数?
【发布时间】:2019-12-02 04:51:34
【问题描述】:

我想在 Asp .net core web api POST 方法中传递很长的字符串作为参数,但是当我传递它时,它只需要开始几个词。如何解决这个问题?

我传递的数据:

{
"Stack":"  RtrnAddr            Arg#1             Arg#2             Arg#3             Arg#4               Function or address
  -----------------   ----------------- ----------------- ----------------- -----------------   -------------------------------------------------------------------
  00000000`378265ef0 : 00000000`00000001 00000000`38e97628 00000000`1efb5b50 00000000`00000000 
  00000000`0058000001 : 00000000`38e97628 00000000`1efb5b50 00000000`00000000 000007fe`ed12964e 
  00000000`38e987628 : 00000000`1efb5b50 00000000`00000000 000007fe`ed12964e ffffffff`fffffffe 
  00000000`1efb58b50 : 00000000`00000000 000007fe`ed12964e ffffffff`fffffffe 00000000`0028d830 
  00000000`000000000 : 000007fe`ed12964e ffffffff`fffffffe 00000000`0028d830 00000000`001e0644 
"
}

【问题讨论】:

    标签: .net api post .net-core asp.net-apicontroller


    【解决方案1】:

    你在api方法的参数中添加了[FromBody]属性吗?

    例如:[FromBody] 字符串值

    请参阅以下链接。可能有用。

    https://www.codeproject.com/Tips/734080/How-to-Pass-a-Large-String-to-a-Web-API-Method-A-2

    【讨论】:

    • 是的,我已添加但仍在邮递员中,我收到状态码 415 的错误
    • 您是否尝试过在邮递员中使用 "Content-Type": "application/json" 标头?
    • 是的,内容类型是""application/json""
    • 您是否也尝试过使用原始格式?然后传递如下数据。{ "data" : { "test1": [ "1", "12" ], "test2": 4, "test3": 7, "test4": 1 } }
    • 以原始格式传递,但仍然存在同样的问题 Jason 值无法转换为 system.string 我在控制器中的 post 方法类似于 [HttpPost] public async TaskPost([FromBody]string Stack ) { }
    【解决方案2】:
    Data you are passing has no issue. It is working. Try this please.
    
    Create another class like this.
    
    public class StackData
        {
            public string Stack{ get; set; }
    }
    
    Then use that class with the parameter in api method as below.
    
    public async Task<IActionResult> Post([FromBody] StackData Stack) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-20
      • 2019-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多