2017-02-18 71 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

非常感謝你 –