尝试在本地 C# web api 中使用 Postman PUT 字符串,始终为 null 或失败

Trying to PUT string with Postman in local C# web api, always null or fails

我目前在本地有这个 WEB API 运行:

// POST api/CsvParse
[HttpPut]
public void Put([FromBody]string value)
{
    if (string.IsNullOrEmpty(value))
        throw new Exception("Input is null or empty.");
}

我目前在本地 运行,我正在使用 POSTMAN 向 put 发送一个字符串。我选择了正文选项卡,并将字符串粘贴到原始正文选项卡中:

它指出我的文本不受支持,或者当我添加断点时该值为空,或者我收到描述格式不正确的错误。

我做错了什么?

将媒体类型更改为 x-www-form-urlencoded 而不是 multipart/form-data。

此外,WebAPI 对 FromBody 参数有讲究。 http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/

对你来说,我认为这是相关的部分:

  1. [FromBody] parameters must be encoded as =value

The final hurdle remaining is that Web API requires you to pass [FromBody] parameters in a particular format. That’s the reason why our value parameter was null in the previous example even after we decorated the method’s parameter with [FromBody].

Instead of the fairly standard key=value encoding that most client- and server-side frameworks expect, Web API’s model binder expects to find the [FromBody] values in the POST body without a key name at all. In other words, instead of key=value, it’s looking for =value.

This part is, by far, the most confusing part of sending primitive types into a Web API POST method. Not too bad once you understand it, but terribly unintuitive and not discoverable.

尝试添加 text/plain

的内容类型

那是因为没有媒体类型格式化程序可以将原始字符串序列化到您的模型中(您的路由参数具有 [FromBody] 属性)。

一个快速而肮脏的解决方法是直接将您的请求正文作为字符串读取:

[HttpPut]
public async Task<HttpResponseMessage> Put(HttpRequestMessage request)
{
    var myCsv = await request.Content.ReadAsStringAsync();

    // do stuff with your string

    return new HttpResponseMessage(HttpStatusCode.OK);
}

作为替代方案,您可以按照 this answer.

自己实施自定义媒体类型格式化程序

有类似的问答here

我发现解决方案 #1 对我有用,因为我试图 PUT JSON 包含 Key/Value 对。所以最初我的 JSON 看起来像这样

{
    "subscriber": {
    "Id": "2",
    "subscriptions":[
        {
            "Name": "Subscription 1",
            "Id": "18",
            "IsSubscribed": false
        },
        {
            "Name": "Subscription 2",
            "Id": "19",
            "IsSubscribed": false
        },
        {
            "Name": "Subscription 3",
            "Id": "20",
            "IsSubscribed": false
        }
    ]
    }
}

但是我修改成了

{
    "Id": "2",
    "subscriptions":[
        {
            "Name": "Subscription 1",
            "Id": "18",
            "IsSubscribed": false
        },
        {
            "Name": "Subscription 2",
            "Id": "19",
            "IsSubscribed": false
        },
        {
            "Name": "Subscription 3",
            "Id": "20",
            "IsSubscribed": false
        }
    ]
}

这奏效了。使用 [FromBody]

在我的 C# web api 中识别了我来自 Postman 的 PUT 请求

补充一下,还有一种解决方案可以将基元传递给 POST 或 PUT 方法。只需将模型指定为 JObject。 ASP.Net 核心网络 api 然后将传入的 JSON 对象(包含字符串等基元)绑定到 JObject 模型对象中。

您的代码如下所示:

// POST api/CsvParse
[HttpPut]
public void Put([FromBody]JObject value)
{
    //access your string data
    string data = value[SPECIFY_KEY_HERE];

    if (string.IsNullOrEmpty(data))
        throw new Exception("Input is null or empty.");
}