2017-08-08 131 views
1

我發現了一個blog post,它顯示瞭如何以字符串形式接收POST JSON。什麼是ASP.NET核心相當於HttpRequestMessage?

我想知道什麼是應該做同樣的事情在REST Post方法下面的代碼在一個控制器的新土辦法:

public async Task<HttpResponseMessage> Post(HttpRequestMessage request) 
{ 
    var jsonString = await request.Content.ReadAsStringAsync(); 

    // Do something with the string 

    return new HttpResponseMessage(HttpStatusCode.Created); 
} 

另一種選擇波紋管不工作對我來說,我覺得因爲我在請求頭中沒有使用Content-Type: application/json(不能改變這個),我得到了415。

public HttpResponseMessage Post([FromBody]JToken jsonbody) 
{ 
    // Process the jsonbody 

    return new HttpResponseMessage(HttpStatusCode.Created); 
} 

回答

1

在.NET中的核心,他們已合併的Web API和MVC所以你也可以做這樣的與IActionResul或其衍生物之一。

public IActionResult Post([FromBody]JToken jsonbody) 
{ 
    // Process the jsonbody 

    return Created("", null);// pass the url and the object if you want to return them back or you could just leave the url empty and pass a null object 
} 
相關問題