在POST参数中接收空值

Sam*_*Sam 5 .net c# asp.net asp.net-web-api

我总是在web api rest post请求到控制器时收到一个空值

在我的控制器中

[HttpPost]
public HttpResponseMessage PostCustomer([FromBody]Customer customer)
{

       System.Diagnostics.Debug.WriteLine(customer); #CustomerApp.Models.Customer
       System.Diagnostics.Debug.WriteLine(customer.FirstName); #null

}
Run Code Online (Sandbox Code Playgroud)

模型

public class Customer
{
    public int Id { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

请求:

POST: http://localhost:21894/api/customer/postcustomer

Content-Type: application/json

body: {FirstName: "xxxx", LastName: 'yyyy'}
Run Code Online (Sandbox Code Playgroud)

我尝试了以下解决方案但没有任何效果

https://myadventuresincoding.wordpress.com/2012/06/19/c-supporting-textplain-in-an-mvc-4-rc-web-api-application/

如何在WebAPI中获取POST数据?

任何人都可以通过帮助或正确的链接指导我

答: 提出卷曲请求而不是与邮递员打交道就像这样给了我解决方案

$ curl -H "Content-Type: application/json" -X POST -d '{"FirstName":"Jefferson","LastName":"sampaul"}' http://localhost
:21894/api/customer/postcustomer
Run Code Online (Sandbox Code Playgroud)

Rah*_*hul 5

我认为你的意思是将Customer对象作为输入而不是像下面那样string

public HttpResponseMessage PostCustomer([FromBody]Customer customer)
 {
Run Code Online (Sandbox Code Playgroud)

好吧,让以下更改并尝试重新发布请求.它应该工作

public HttpResponseMessage PostCustomer(Customer customer)
 {
   return OK(customer.FirstName);
 }
Run Code Online (Sandbox Code Playgroud)

请求:

POST: http://localhost:21894/api/customer/postcustomer
Content-Type: application/json; charset=utf-8
{"Id":101,"FirstName":"xxxx","LastName":'yyyy'}
Run Code Online (Sandbox Code Playgroud)