【发布时间】:2017-09-27 14:28:18
【问题描述】:
我的控制器无法通过POST 方法接受字符串。有什么问题?当我创建HttpClient 并发送这样的内容时:
var content = new FormUrlEncodedContent(new []
{
new KeyValuePair<string, string>("signature", "someexamplecontent"),
});
var response = await _client.PostAsync(path, content);
我收到一个错误:415, Unsupported media type,它没有进入控制器。相反,当我使用PostAsJsonAsync - 进入但参数signature 为空。
var response = await _client.PostAsJsonAsync(path, content);
这是控制器中的一个方法:
[HttpPost("generatecert")]
public byte[] PostGenerateCertificate([FromBody] string signature)
{
}
【问题讨论】:
-
您是否检查过请求是否发送了正确的
Content-Type和Content-Encoding标头,并确保服务器接受“application/x-www-form-urlencoded”内容类型?这是您接收 POST 数据的唯一操作吗?
标签: c# asp.net-mvc asp.net-web-api dotnet-httpclient