C# 在FromBody中作为参数接收PostAsync

C# 在FromBody中作为参数接收PostAsync,c#,asp.net-mvc-4,asp.net-web-api,dotnet-httpclient,C#,Asp.net Mvc 4,Asp.net Web Api,Dotnet Httpclient,我正在尝试从Web API控制器读取通过HttpClient.PostAsync()方法发送的JSON字符串。但是由于某种原因,RequestBody总是null 我的请求如下所示: public string SendRequest(string requestUrl, StringContent content, HttpMethod httpMethod) { var client = new HttpClient { BaseAddress = new Uri(ServerUrl

我正在尝试从Web API控制器读取通过
HttpClient.PostAsync()
方法发送的JSON字符串。但是由于某种原因,
RequestBody
总是
null

我的请求如下所示:

public string SendRequest(string requestUrl, StringContent content, HttpMethod httpMethod)
{
    var client = new HttpClient { BaseAddress = new Uri(ServerUrl) };
    var uri = new Uri(ServerUrl + requestUrl); // http://localhost/api/test

    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 

    HttpResponseMessage response;
    response = client.PostAsync(uri, content).Result;

    if (!response.IsSuccessStatusCode)
    {
        throw new ApplicationException(response.ToString());
    }

    string stringResult = response.Content.ReadAsStringAsync().Result;
    return stringResult;
}
[HttpPost]
public string PostContract()
{
    string httpContent = Request.Content.ReadAsStringAsync().Result;
    return httpContent;
}
[HttpPost]
public string PostContract([FromBody] string httpContent)
{
    return httpContent;
}

我这样称呼这个方法

var content = new StringContent(JsonConvert.SerializeObject(testObj), Encoding.UTF8, "application/json");
string result = Request.SendRequest("/api/test", content, HttpMethod.Post);

现在,我的Web API控制器方法读取的发送数据如下:

public string SendRequest(string requestUrl, StringContent content, HttpMethod httpMethod)
{
    var client = new HttpClient { BaseAddress = new Uri(ServerUrl) };
    var uri = new Uri(ServerUrl + requestUrl); // http://localhost/api/test

    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 

    HttpResponseMessage response;
    response = client.PostAsync(uri, content).Result;

    if (!response.IsSuccessStatusCode)
    {
        throw new ApplicationException(response.ToString());
    }

    string stringResult = response.Content.ReadAsStringAsync().Result;
    return stringResult;
}
[HttpPost]
public string PostContract()
{
    string httpContent = Request.Content.ReadAsStringAsync().Result;
    return httpContent;
}
[HttpPost]
public string PostContract([FromBody] string httpContent)
{
    return httpContent;
}
这个很好用。
stringResult
属性包含控制器方法返回的字符串。但我希望我的控制器方法如下:

public string SendRequest(string requestUrl, StringContent content, HttpMethod httpMethod)
{
    var client = new HttpClient { BaseAddress = new Uri(ServerUrl) };
    var uri = new Uri(ServerUrl + requestUrl); // http://localhost/api/test

    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 

    HttpResponseMessage response;
    response = client.PostAsync(uri, content).Result;

    if (!response.IsSuccessStatusCode)
    {
        throw new ApplicationException(response.ToString());
    }

    string stringResult = response.Content.ReadAsStringAsync().Result;
    return stringResult;
}
[HttpPost]
public string PostContract()
{
    string httpContent = Request.Content.ReadAsStringAsync().Result;
    return httpContent;
}
[HttpPost]
public string PostContract([FromBody] string httpContent)
{
    return httpContent;
}
请求似乎正在工作,得到了
200-OK
,但是
SendRequest
方法的
stringResult
总是
null


为什么我使用
RequestBody
作为参数的方法不起作用?

既然您是作为
application/json
发布的,框架正在尝试对其进行反序列化,而不是提供原始字符串。无论示例中的
testObj
的类型是什么,请将该类型用于控制器操作参数和返回类型,而不是
string

[HttpPost]
public MyTestType PostContract([FromBody] MyTestType testObj)
{
    return testObj;
}

你忘了告诉我们实际的内容。使用
[FromBody]
@MatíasFidemraizer,给出您想要获取的示例内容。我添加了我调用
SendRequest
方法的代码。我基本上是将一个对象转换为一个
JSON
字符串,并将其作为原始内容发送。这不是在正文中寻找一个名为httpContent的表单变量吗?我想你可以使用一个自定义绑定器来实现,它基本上与你的工作代码相同example@ToddMenier这确实是问题所在。我将JSON发送到我的控制器,但控制器希望将其转换回一个对象。似乎您不能简单地获取JSON字符串本身。这对我来说没关系。因此,将参数更改为
PostContract([FromBody]Contract Contract)
成功了。你能不能把它作为一个答案贴出来,这样我就可以接受了。