Loading

netcore 对 Action 方法进行模型绑定参数注意的细节

netcore 对 Action 方法进行模型绑定参数注意的细节:
1.mvc控制器。一般继承的是 Controller (Controller 里面包含视图),对 action 方法参数 模型绑定的时候,参数的推断来源默认是 从表单中获取 相当于默认有 [FromFrom] 这个特性加在参数旁边
 
2.webapi,继承 ControllerBase(里面没有关于视图的方法),webapi 一般加个特性 [ApiController] ,如果加了这个特性,对 action 方法参数 模型绑定的时候,参数的推断来源默认是 从主体中获取 相当于默认有 [FromBody] 这个特性加在参数旁边
 
使用Content-Type: application/x-www-form-urlencoded; charset=UTF-8 来编码

 

 使用 Content-Type: application/json 来编码

 

 

 1 /// <summary>
 2 /// 多参数获取,数据通过json序列化后都在RequestPayload中,实体参数与普通参整成一个
 3 /// </summary>
 4 /// <param name="jData"></param>
 5 /// <returns></returns>
 6  [HttpPost]
 7  [Route("/get/kj")]
 8  public IActionResult io([FromBody]Newtonsoft.Json.Linq.JObject jData)
 9  {
10             // function ee() {
11             //     var model = { a: 1, b: 2,c: 4,};
12             //     var arr = [{ "a": 1, "b": 1, "c": 2, "c" : 3 }];15             //     var khg =
16             //     {
17             //            k: arr,   //直接数组不可以的,后端用 Jobject接受到了,序列化会导致报错
18             //            //k: model,  //直接写json对象是可以的
19             //            yy: 34,
20             //            bb: "好呀",
21             //     };23             //        $.ajax({
24             //            type: "post",
25             //            url: "/get/kj",
26             //            data: JSON.stringify(khg),
27             //            contentType: "application/json;charset=utf-8"
28             //        });
29             //  }
30             //dynamic json = jData;
31             //Newtonsoft.Json.Linq.JObject jUser = json.k;
32             //KJ kmmm = jUser.ToObject<KJ>();
33             //int yy = json.yy;
34             //string bb = json.bb;
35 36    return Json("OK");
37 }
38

 

 

 
posted @ 2019-11-10 12:41  大意了啊  阅读(1113)  评论(0编辑  收藏  举报