当前台传参为Requset Payload时:
后台需要在参数列表中加[FromBody]
[Route("/create"))] public string Create([FromBody] Model model) { }
或者用流来进行接收
using (var bufferStream = new MemoryStream())
{
Request.Body.CopyToAsync(bufferStream);
byte[] buffer = bufferStream.ToArray();
string param = System.Text.Encoding.UTF8.GetString(buffer);
Model model = JsonConvert.DeserializeObject<Model>(param);
}
当前台传参为FormData时:
直接在参数列表中映射实体类
[Route("/create"))] public string Create(Model model) { }
两者的区别:
当POST请求的请求头里设置Content-Type: application/x-www-form-urlencoded(ajax默认), 参数在请求体以标准的Form Data的形式提交,以&符号拼接,参数格式为key=value&key=value&key=value...