【问题标题】:HTTP POST not working in OWIN Self-Host Web APIHTTP POST 在 OWIN 自托管 Web API 中不起作用
【发布时间】:2015-09-02 15:34:45
【问题描述】:

我自己托管一个 Web API。我从集成测试中提出的任何 Get Reqeust 都可以正常工作。但是,任何 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"));
}

控制器

public string Post(string value)
{
    return "Hello!";
}

【问题讨论】:

    标签: c# .net asp.net-web-api2 owin self-hosting


    【解决方案1】:

    我遇到了同样的问题,发现有必要在方法参数之前使用 [FromBody] 属性。在这种情况下,它可以解析请求有效负载并且您可以访问您的方法

    希望有帮助

    【讨论】:

      【解决方案2】:

      可能是你错过了 / on:

      using (WebApp.Start<App_Start.TestConfiguration>("http:/localhost:8216"))
      

      应该是http://localhost:8216

      【讨论】:

      • 是的,这让我经历了一部分。现在我有一个 400 错误:(
      猜你喜欢
      • 2019-02-24
      • 2015-01-09
      • 1970-01-01
      • 2016-12-15
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多