【问题标题】:How to pass json string to web api c#如何将json字符串传递给web api c#
【发布时间】:2021-07-04 00:55:54
【问题描述】:

我正在尝试将字符串传递给 web api 方法。字符串是json字符串。但是当通过提琴手传递下面的字符串时,只有部分字符串被传递。

"{"姓名": "玛纳斯", "年龄": "28"}"

当只传递上面的字符串"{"这部分被web api接收。

请您确认这里出了什么问题。

下面是web api。

[HttpPost]
public void Method1([FromBody]string formData){
    string encrypted = Security.Security.Encrypt(formdata);
}

【问题讨论】:

    标签: c# asp.net json asp.net-mvc fiddler


    【解决方案1】:

    确保您在 fiddler 中的内容类型是 json。示例:

    User-Agent: Fiddler
    Host: localhost:11111
    Content-Type: application/json; charset=utf-8
    Content-Length: 83
    

    在你的身体中也传递 json 没有 "

    {"Name": "Manas", "Age": "28"}
    

    编辑:抱歉,我没有注意到您需要一个字符串...如果您真的需要,您可以这样做:

    [HttpPost]
    public void Method1([FromBody]string formData){
        string data = new StreamReader(HttpContext.Current.Request.InputStream).ReadToEnd();
        string encrypted = Security.Security.Encrypt(data);
    }
    

    【讨论】:

    • 不,这不起作用,它将被视为对象,并且方法中的参数也是方法
    • 更新了我的答案。
    猜你喜欢
    • 1970-01-01
    • 2015-07-01
    • 2021-04-05
    • 2023-04-04
    • 1970-01-01
    • 2012-11-22
    • 2019-11-16
    • 2018-11-25
    • 1970-01-01
    相关资源
    最近更新 更多