2017-02-18 90 views
1

我正在使用WebApi.SelfHost但我的帖子始终为空。 我尝试了所有可以找到的解决方案,但都没有成功。c#WebApi.SelfHost post is null

我被这个问题困住了好几个小时。 我尝试了所有我能找到的解决方案,但都没有成功。

我知道这个问题已经被问到。但我已经尝试过所有的解决方案,但他们都没有工作。而且他们没有使用自己的主机。

这是我的代码。

class Program 
{ 
    static void Main(string[] args) 
    { 
     var config = new HttpSelfHostConfiguration("http://localhost:8080"); 

     config.Routes.MapHttpRoute(
      "API Default", "api/{controller}/{action}/{id}", 
      new { id = RouteParameter.Optional }); 

     using (HttpSelfHostServer server = new HttpSelfHostServer(config)) 
     { 
      server.OpenAsync().Wait(); 
      Console.WriteLine("Press Enter to quit."); 
      Console.ReadLine(); 
     } 
    } 
} 

public class testController : ApiController 
{ 
    [HttpPost] 
    public string test(int id ,[FromBody]string value) 
    { 
     return value; 
    } 
} 

我使用Advanced REST client测试

这里是我的结果

enter image description here

回答

1

您输入的字符串值到的请求头部分。您应该在请求的主体中发送该值。例如。把头部

Content-Type: application/json 

并添加主体(下文头部分,名为原始负载

"testValue" 

注意:考虑使用Postman

+0

非常感谢你 –