自从升级到RC的WebAPI,我有一些真正奇怪的问题时调用POST在我的WebAPI。 我甚至回到了在新项目上生成的基本版本。所以:

public void Post(string value)
{
}

提琴手喊道:

Header:
User-Agent: Fiddler
Host: localhost:60725
Content-Type: application/json
Content-Length: 29

Body:
{
    "value": "test"
}

当我调试时,字符串“value”永远不会被赋值。它总是NULL。 有人有这个问题吗?

(我第一次看到这个问题是在一个更复杂的类型上)

这个问题不仅仅局限于ASP。在asp.net MVC 4中,同样的问题出现在一个新的ASP。NET MVC 3项目后RC安装


当前回答

我尝试了这个帖子中的许多答案,但没有一个对我有用。然后我在一个类似的帖子中看到了这个答案:https://stackoverflow.com/a/40853424/2120023,他提到HttpContext. request . body,所以另一个搜索,我发现这个https://stackoverflow.com/a/1302851/2120023给了我HttpContext。当前,所以我终于得到了这个工作使用:

HttpContext.Current.Request.Form.Get("value");

邮差的要求:

curl --location --request POST 'https://example.com/token' --header 'Content-Type: application/x-www-form-urlencoded' --data-urlencode 'value=test'

其他回答

在我的例子中,用Newtonsoft的[JsonObject(memberseriizable . optout)]属性装饰参数类就很管用。

例如:

[HttpPost]
[Route("MyRoute")]
public IHttpActionResult DoWork(MyClass args)
{
   ...
}

[JsonObject(MemberSerialization.OptOut)]
public Class MyClass
{
    ...
}

在ASP中也遇到过类似的问题。NET Core和另一个可能的原因是ASP。NET绑定(沉默)失败,由于各种原因,如发送null被绑定到一个非null属性:

{
    "prop1":1139357,
    "prop2":1139356,

    "items":[
        {"key":"15","someprop":34,"notnullprop":null},
        {"key":"16","someprop":34,"notnullprop":null},
        {"key":"22","someprop":34,"notnullprop":null}]
}

在这种情况下,不会抛出异常,整个模型将为null,即使这种情况发生在对象层次结构的深处。

我刚刚使用Fiddler发生了这种情况。问题是我没有指定Content-Type。

尝试在POST请求中包含Content-Type头。

Content-Type: application/x-www-form-urlencoded

或者,根据下面的注释,您可能需要包含一个JSON头

Content-Type: application/json

如果你在Postman的json查询中有这个字符'\',你应该转义它,如果你需要在json字符串值中使用'\'字符。

例子:

{
    "path": "C:\\Users\\Test\\Documents\\test.txt"
}

我有点晚了,但是任何人在使用控制器时偶然发现一个NULL值,只要在POST请求的前面添加“=”就可以了。

当我使用application/json Content-Type时,控制器也传递了一个NULL值。注意下面的“application/x-www-form-urlencoded”内容类型。然而,API的返回类型是“application/json”。

 public static string HttpPostRequest(string url, Dictionary<string, string> postParameters)
    {
        string postData = "=";

        foreach (string key in postParameters.Keys)
        {
            postData += HttpUtility.UrlEncode(key) + "="
                  + HttpUtility.UrlEncode(postParameters[key]) + ",";
        }

        HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
        myHttpWebRequest.Method = "POST";

        byte[] data = System.Text.Encoding.ASCII.GetBytes(postData);

        myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
        myHttpWebRequest.ContentLength = data.Length;

        Stream requestStream = myHttpWebRequest.GetRequestStream();
        requestStream.Write(data, 0, data.Length);
        requestStream.Close();

        HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

        Stream responseStream = myHttpWebResponse.GetResponseStream();

        StreamReader myStreamReader = new StreamReader(responseStream, System.Text.Encoding.Default);

        string pageContent = myStreamReader.ReadToEnd();

        myStreamReader.Close();
        responseStream.Close();

        myHttpWebResponse.Close();

        return pageContent;
    }