.netCore WebApi the general previous MVc5, inherited from ApiController, is WebApi, using a unique pipe handling model, Core, and then, they are usually inherited from ControllerBase , denoted controller class [ApiController]
1. The front-end code
. 1 // NetCoreWebApi, the transmission application / json; format, the rear end without adding [FormBody] can get the value 2 $ .ajax ({ . 3 type: "POST" , . 4 URL: "/ API / WebApi / WW" , . 5 Data: the JSON.stringify ({A:. 1, B: 2, C:. 4, "YY": 99, "BB": "match"}) // if not serialized, not obtain a rear end, serialized, WebApi not need to add FormBody, to get value, contentType: "application / json; charset = utf-8" // default does not form together with the kind that is not json . 6 contentType: "file application / -WWW-form-X urlencoded ", // this type can not be specified . 7 Data: {A:. 1, B: 2, C:. 4," YY ": 99," BB ":" match " }, 8 })
2. The back-end code
[HttpPost] [Route("ww")] public string Po(KJ k, int yy, string bb) { //k null //yy 0 //b null }
3. Conclusion:
Because the front-end is specified, transmission type, "application / x-www-form-urlencoded", if not write contentType, its default value is "application / x-www-form-urlencoded", but failed to specify
Last Request format: application / problem + json; charset = utf-8
Being given: 415
Resolution: json designated as a transmission, after transmission of the sequence, the rear end can get value (can not [FormBody]), in the core of the transmission contentType
[HttpPost] [the Route ( " E3 " )] public String fdasfs ([BindAttribute (the include: " A, B " )] [FromBody] KJ P, int R & lt = 0 ) { // *************************************** *********. use of Net Core Mvc WebApi post of **************************************************** / // 1. distal specified file application / json, json data transfer, with or without [FromBody] can get the value / * 2. the front end of the specified application / x-www-form- urlencoded, or data transmission json json object fails * obviously provided a transmission type, but it became application / problem + json; charset = utf-8, request fails 3. If the incoming entity addition, there are other parameters, that are not bound on the value did not get is such that the front end: {user: {a: 1 , b: 9}, r: 99} is not enough, {a:. 1, B:} feasible. 9 Entity as a whole must / JSON = {var * User: {A:. 1, B:. 9,}, R & lt: 99}; $ .ajax ({ URL: "/ API / mm / E3", type: "POST" , // contentType: "file application / X-WWW-form-urlencoded", // write this, write and white writing, eventually covered by transmission type contentType: "file application / JSON", Data: the JSON.stringify (JSON) }); * / / * BindAttribute characteristics 4.NetCore in contentType: "application / json ineffective, can not bind the attributes specified in contentType:" application / x-www -form-urlencoded form can, filters out unwanted properties of the FormData a:. 1 B:. 9 * / return " the OK " ; }