本文介绍了HTTP POST在OWIN自托管Web API中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是自我托管Web API的人.我从集成测试中获得的任何Get Reqeust都可以正常工作.但是,任何POST请求都将引发连接被拒绝.我似乎无法掌握正在发生的事情.
I am self self hosting a Web API. Any Get Reqeust I make from my integration tests works fine. However any POST request throws connection refused. I can't seem to get a handle on what is happening.
错误消息
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"));
}
控制器
public string Post(string value)
{
return "Hello!";
}
推荐答案
我遇到了同样的问题,发现有必要在方法参数之前使用[FromBody]属性.在这种情况下,它可以解析请求有效负载,并且您可以访问方法
I've got the same issue and found that it is necessary to use [FromBody] attribute before your method params. In this case it can parse request payload and you can reach your methods
希望有帮助
这篇关于HTTP POST在OWIN自托管Web API中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!