应在后台创建包含参数的结构体(注意字段名称要一一对应)

时间:2021-11-06 08:14:36

1.HttpPOST要领中,,参数前添加[FromBody]

[HttpPost]

public string PostMethod([FromBody]string paras)

{

...

}

2.HttpPOST要领不撑持多个参数,如果前台传入多个参数,应在后台创建包罗参数的布局体(注意字段名称要一一对应),HttpPOST要领的参数为该布局体类型

错误要领

[HttpPost]

public string PostMethod([FromBody]string user,string pwd)

{

...

}

正确要领

public Struct User

{

  public string user {get; set;}

  public string pwd {get; set;}

}

[HttpPost]

public string PostMethod([FromBody]User userInfo)

{

...

}

3 注意注意!

public Struct User

{

  public string user;

  public string pwd;

}  这样是错的!!!必然要是属性!!!

public Struct User

{

  public string user {get; set;}

  public string pwd {get; set;}

}  这样才可以!!  

WebAPI HttpPOST参数为null