Implementing the HTTP method in the Web API (Get,put,post,delete)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.