已发布的 jSon 对象在 api 控制器中收到时为空
Posted jSon object is null when received in api controller
我尝试使用 restSharp 检索我作为 jSon 发送到我的 API 控制器的模型,但是当我在我的控制器中接收到它时,TestCard 对象始终为 null,这是为什么?
型号
public class TestCard
{
public int OrderId { get; set; }
public List<Person> Persons { get; set; }
}
public class Person
{
public string Name { get; set; }
public string Adress { get; set; }
public string Adress2 { get; set; }
public string PostalCode { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
测试Post
var personList = new List<Person>();
personList.Add(new Person
{
Name = "John Doe",
Adress = "West Park Ave",
PostalCode = "123",
City = "NY",
Country = "US"
});
personList.Add(new Person
{
Name = "Sandra Doe",
Adress = "West Park Ave",
PostalCode = "123",
City = "NY",
Country = "US"
});
var order = new TestCard() { OrderId = 1, Persons = personList };
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var client = new RestClient("https://localhost:44336/api/card/");
client.Authenticator = new HttpBasicAuthenticator("myusername", "mypassword");
var request = new RestRequest("Add", Method.Post);
request.AddBody(JsonConvert.SerializeObject(order));
request.AddHeader("Content-Type", "application/json");
Task<RestResponse> response = client.ExecuteAsync(request);
if (response.Result.StatusCode == HttpStatusCode.Created)
{
Response.Write("OK!");
}
控制器
[Route("api/Card/Add")]
[AcceptVerbs("GET", "POST")]
[HttpPost]
public IHttpActionResult Add([FromBody] TestCard model)
{
return Ok();
}
当您使用 request.addbody 时,您不必序列化对象
request.AddBody(order);
和恕我直言,删除 [AcceptVerbs("GET", "POST")] 因为您已经 [HttpPost]
我尝试使用 restSharp 检索我作为 jSon 发送到我的 API 控制器的模型,但是当我在我的控制器中接收到它时,TestCard 对象始终为 null,这是为什么?
型号
public class TestCard
{
public int OrderId { get; set; }
public List<Person> Persons { get; set; }
}
public class Person
{
public string Name { get; set; }
public string Adress { get; set; }
public string Adress2 { get; set; }
public string PostalCode { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
测试Post
var personList = new List<Person>();
personList.Add(new Person
{
Name = "John Doe",
Adress = "West Park Ave",
PostalCode = "123",
City = "NY",
Country = "US"
});
personList.Add(new Person
{
Name = "Sandra Doe",
Adress = "West Park Ave",
PostalCode = "123",
City = "NY",
Country = "US"
});
var order = new TestCard() { OrderId = 1, Persons = personList };
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var client = new RestClient("https://localhost:44336/api/card/");
client.Authenticator = new HttpBasicAuthenticator("myusername", "mypassword");
var request = new RestRequest("Add", Method.Post);
request.AddBody(JsonConvert.SerializeObject(order));
request.AddHeader("Content-Type", "application/json");
Task<RestResponse> response = client.ExecuteAsync(request);
if (response.Result.StatusCode == HttpStatusCode.Created)
{
Response.Write("OK!");
}
控制器
[Route("api/Card/Add")]
[AcceptVerbs("GET", "POST")]
[HttpPost]
public IHttpActionResult Add([FromBody] TestCard model)
{
return Ok();
}
当您使用 request.addbody 时,您不必序列化对象
request.AddBody(order);
和恕我直言,删除 [AcceptVerbs("GET", "POST")] 因为您已经 [HttpPost]