我自行托管 Web API。我从集成测试中发出的任何获取请求都可以正常工作。但是,任何 POST 请求都会引发连接被拒绝。我似乎无法掌握正在发生的事情。
错误消息
system.Net.HttpRequestException: An error occured while sending the request to the remote server. SocketException: no connection could be made because the target machine actively refused.
代码
using (WebApp.Start<App_Start.TestConfiguration>("http:/localhost:8216"))
{
var client = new HttpClient();
client.BaseAddress = new System.Uri("http://127.0.0.1:8216");
var response = await client.PostAsync("/api/MyController", new StringContent("something"));
}
Controller
public string Post(string value)
{
return "Hello!";
}
最佳答案
我遇到了同样的问题,发现有必要在方法参数之前使用 [FromBody] 属性。在这种情况下,它可以解析请求负载,您可以访问您的方法
希望有帮助
关于c# - HTTP POST 在 OWIN 自托管 Web API 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32357354/