参考地址
1.前台通过 Json提交,后台通过 [FromBody]实体 value 接收
JS Json提交
var settings = {
"url": "http://localhost:5000/api/Login/post4",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/json"
},
"data": JSON.stringify({"Code":"111","Name":"张三"}),
};
$.ajax(settings).done(function (response) {
console.log(response);
});
.Net Core 接收
[HttpPost]
public IActionResult post4([FromBody]BaseUser value)
{
return new JsonResult(value);
}
2.前台通过 Json提交,后台通过 dynamic value 接收
dynamic详细介绍
dynamic 简单介绍,就是一个弱类型,参考JS中的var。
[HttpPost]
public IActionResult test(dynamic value)
{
string b = value.ToString();//两种转换都可以,但是 string b = value;不行
var a = Convert.ToString(value);
Newtonsoft.Json.Linq.JObject jobject = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(a);
return new JsonResult(JsonConvert.SerializeObject(jobject));
}
3.补充说明
原本是想后台通过 Jobject value 接收,发现报错。
但是在.Net api
里面解析没问题。可能是Core里面字典的Json版本较新的问题吧
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|a7b40305-495e98103e7856d8.",
"errors": {
"$": [
"The JSON value could not be converted to System.Collections.Generic.IEnumerable`1[Newtonsoft.Json.Linq.JToken]. Path: $ | LineNumber: 0 | BytePositionInLine: 6."
]
}
}