Post (new), Put (modify), delete (delete), Get (query)
Get: Raw to Data list (default), or get an Entity data
POST: Add a Service side to add a record that records the entity as a Form object
PUT: Add or modify a record on the service side, record the entity's Form object, and record the primary key to be transferred in a Get mode
Delete: Deletes a record on the service side
The API interfaces exposed above are called in the XMLHttpRequest case, and of course you can use jquery's Ajax components to complete this request call
<fieldset> <legend>testing the Web Api</legend> <a href="Javascript:add ()"> Add (POST) </a> <a href="javascript:update (1)"> Update (Put) </a> <a href="javascript:deletes (1)"> Remove (delete) </a> <a href="/api/test"> List (Get) </a> <a href="/API/TEST/1"> Entities (Get) </a></fieldset><script>function Add () {$.ajax ({URL:"/api/test/", type:"POST", data: {"UserID":4,"UserName":"Test","UserEmail":"[email protected]"}, Success:function (data) {alert (json.stringify (data));} }); } //Updatefunction Update (ID) {$.ajax ({URL:"/api/test?id="+ID, type:"Put", data: {"UserID":1,"UserName":"moditest","UserEmail":"[email protected]"}, Success:function (data) {alert (json.stringify (data));} }); } function deletes (ID) {$.ajax ({URL:"/API/TEST/1", type:"DELETE", Success:function (data) {alert (data);} }); }</script>
/// <summary> ///Test Module API///uri:/api/test/// </summary> Public classTestcontroller:apicontroller {/// <summary> ///User Data List/// </summary> Private ReadOnlyList<users> _userlist =NewList<users> { NewUsers {UserID =1, UserName ="Zzl", UserEmail ="[email protected]"}, NewUsers {UserID =2, UserName ="Spiderman", UserEmail ="[email protected]"}, NewUsers {UserID =3, UserName ="Batman", UserEmail ="[email protected]"} }; /// <summary> ///Get List Object/// </summary> /// <returns></returns> PublicIenumerable<users>Get () {return_userlist; } /// <summary> ///get an entity, depending on the primary key/// </summary> /// <param name= "id" ></param> /// <returns></returns> PublicUsers Get (intID) {return_userlist.firstordefault (i = I.userid = =ID); } /// <summary> ///Add/// </summary> /// <param name= "form" >Form object, which is unique</param> /// <returns></returns> PublicThe users Post ([Frombody] users entity) {_userlist.add (entity); returnentity; } /// <summary> ///Update/// </summary> /// <param name= "id" >PRIMARY Key</param> /// <param name= "form" >Form object, which is unique</param> /// <returns></returns> PublicUsers Put (intID, [frombody]users entity] { varuser = _userlist.firstordefault (i = I.userid = =)ID); if(User! =NULL) {User. UserName=entity. UserName; User. UserEmail=entity. UserEmail; } returnuser; } /// <summary> ///Delete/// </summary> /// <param name= "id" >PRIMARY Key</param> /// <returns></returns> Public voidDelete (intID) {_userlist.remove (_userlist.firstordefault (i=>i.userid==id)); } }
Implementing the HTTP method in the Web API (Get,put,post,delete)