为了账号安全,请及时绑定邮箱和手机立即绑定

如何将JSON POST数据作为对象传递给WebAPI方法?

如何将JSON POST数据作为对象传递给WebAPI方法?

子衿沉夜 2019-07-08 15:17:57
如何将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"}
查看完整描述

3 回答

  • 3 回答
  • 0 关注
  • 3626 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信