如何将JSON POST数据作为对象传递给WebAPI方法?ASP.NETMVC4WebAPI应用程序定义POST方法以保存客户。客户在POST请求体中以json格式传递。POST方法中的Customer参数包含属性的空值。如何解决这个问题,以便将已发布的数据作为客户对象传递?如果可能的话-type:application/x-www-form-urlencode应该使用,因为我不知道如何在发布表单的javascript方法中更改它。主计长:public class CustomersController : ApiController {
public object Post([FromBody] Customer customer)
{
return Request.CreateResponse(HttpStatusCode.OK,
new
{
customer = customer });
}
}}public class Customer
{
public string company_name { get; set; }
public string contact_name { get; set; }
}请求:POST http://localhost:52216/api/customers HTTP/1.1Accept: application/json, text/javascript, */*; q=0.01
X-Requested-With: XMLHttpRequest
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
{"contact_name":"sdfsd","company_name":"ssssd"}
添加回答
举报
0/150
提交
取消