And we know that the Post,put method can only have one frombody parameter, and then there are multiple parameters, it is mentioned that it needs to be encapsulated as an object to pass, and this is mainly around this topic, the interface layer to add a new class User_info, for data transfer, And the client uses the Web page AJAX and the console httpclient the way to implement separately, follow me!
The following defines a complex type object
public class User_info {public int Id {get; set;} public string Name {get; set;} public string Info {get; set;} }
Modify the last API section below to let it manipulate the object
[Corsattribute ("http://localhost:3321")] public class Registercontroller:apicontroller {public static list<user_info> Model = new list<user_info> () {new user_info{id=1,name= "Zzl", info= "Zzl is Landlord"} , new User_info{id=2,name= "Zhz", info= "Zhz is the son of Zzl"}, New User_info{id=3,name= "ZQL", info= "ZQL is Zzl's Wife"} , new User_info{id=4,name= "Bobo", info= "Bobo is Zzl's friend"}; Get api/values public ienumerable<user_info> get () {return Model; }//Get API/VALUES/5 public user_info get (int id) {var entity = Model.firstordefault (i = > i.id = = Id); return entity; }//Get api/values/5?leval=1 public httpresponsemessage get (int id, int leval) {return n EW Httpresponsemessage (httpstatuscode.ok) {Content = new Stringcontent ("<em style=" color:red ' > Successful response (id,level) </EM≫ ", System.Text.Encoding.UTF8," text/html ")}; }//Post Api/values public httpresponsemessage post ([Frombody]user_info value) {Model.ad D (New User_info {Id = value. Id, Info = value. Info, Name = value. Name,}); User Login related return new Httpresponsemessage (Httpstatuscode.ok) {Content = new Stringcont ENT ("Add data success, User ID:" + value.) Id, System.Text.Encoding.UTF8, "Text/plain")}; }//Put api/values?userid=5 public httpresponsemessage put (int userid, [frombody]user_info value) { var entity = Model.firstordefault (i = i.id = = userid); Entity. Info = value. Info; Entity. Name = value. Name; return new Httpresponsemessage (Httpstatuscode.ok) {Content = new Stringcontent ("Modify data succeeded, primary key:" + U Serid + ", object:" + value. Name)}; }//delete api/values/5 public httpresponsemessage delete (int id) {Model.remove (Mod El. FirstOrDefault (i = i.id = = Id)); return new Httpresponsemessage (Httpstatuscode.ok) {Content = new stringcontent ("delete data succeeded") }; }
And the most critical place is at the time of each client invocation, first of all, you can't expect the client to reference your assembly, because the platform cannot implement such a reference (Java & C#,js & c#,php & C #), so you need to have their own method at the time of invocation, And JS Ajax call, directly using the JSON object, the key name object
Entity properties, when using HttpClient, directly assign a set of key-value pairs to the Formurlencodedcontent object, as described below
The JS implementation of HTML
$.ajax ({ URL: "Http://localhost:52824/api/register", Type: "POST", data: {id:5, Name: ' New ', Info: ' Good for everyone '} ,///Here the key name must be empty, multiple parameters please pass the object, API side parameter name must be value success:function (data) { Console.log ("Post:" + data); } ); $.ajax ({ URL: "Http://localhost:52824/api/register", Type: "GET", success:function (data) { For (var i in data) { console.log (Data[i]. Id + "" + data[i]. Name);}} );
Results
Implementation using HttpClient objects in the console program
<summary>//HttpClient implement POST request///</summary> static async void Doopost () {String url = "Http://localhost:52824/api/register"; Set Httpclienthandler automaticdecompression var handler = new Httpclienthandler () {automaticdecompression = D Ecompressionmethods.gzip}; Create HttpClient (note incoming httpclienthandler) using (var http = new HttpClient (handler)) {// Use Formurlencodedcontent to do httpcontent var content = new Formurlencodedcontent (new dictionary<string, Strin G> () {"Id", "6"}, {"Name", "Add Zzl"}, {"Info", "Add action"}/ /Key name must be empty}); Await async wait response var response = await http. Postasync (URL, content); Ensure that the HTTP success status value is response. Ensuresuccessstatuscode (); Await asynchronously reads the last JSON (note that Gzip is now automatically decompressed because the automaticDecompression = Decompressionmethods.gzip) Console.WriteLine (await response. Content.readasstringasync ()); }}///<summary>//httpclient Implement GET request///</summary> static async void do Oget () {String url = "Http://localhost:52824/api/register?id=1"; Create httpclient (note incoming httpclienthandler) var handler = new Httpclienthandler () {automaticdecompression = Decompre Ssionmethods.gzip}; using (var http = new HttpClient (handler)) {//await Asynchronously waits for a response var response = await H Ttp. Getasync (URL); Ensure that the HTTP success status value is response. Ensuresuccessstatuscode (); Await asynchronously reads the last JSON (note that at this point the GZip has been automatically decompressed since automaticdecompression = decompressionmethods.gzip) console.write Line (await response. Content.readasstringasync ()); }}///<summary>//httpclient implement put request///</summary> static async void Dooput () {var userId = 1; String url = "http://localhost:52824/api/register?userid=" + userid; Set Httpclienthandler automaticdecompression var handler = new Httpclienthandler () {automaticdecompression = D Ecompressionmethods.gzip}; Create HttpClient (note incoming httpclienthandler) using (var http = new HttpClient (handler)) {// Use Formurlencodedcontent to do httpcontent var content = new Formurlencodedcontent (new dictionary<string, Strin G> () {"Name", "Modify Zzl"}, {"Info", "put modify action"}//key name must be empty }); Await async wait response var response = await http. Putasync (URL, content); Ensure that the HTTP success status value is response. Ensuresuccessstatuscode (); Await asynchronously reads the last JSON (note that at this point the GZip has been automatically decompressed since automaticdecompression = decompressionmethods.gzip) Console.WriteLine (await response. Content.readasstringasync ()); } }ext.: http://www.cnblogs.com/lori/p/4045633.html
Invoking the Web API interface through HttpClient-continuation ~ Entity parameter delivery