RESTful standards-based web API

Source: Internet
Author: User

Microsoft's Web API is published on the MVC4 project bindings on vs2012, and its proposed Web API is completely based on the restful standard, completely different from the previous (as is the SOAP protocol) WCF and WebService, it is simple, code readable, quick to get started, If I want to compare it with the Web service, I would say that its interface is more standard, clearer, no confusing method name, some only a few standard requests, such as Get,post,put,delete, they correspond to several operations, the following:

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

Note that the API interfaces exposed above are called in the XMLHttpRequest case, and of course you can use jquery's Ajax components to make this request call, its code is more object-oriented, the following examples illustrate

This is the HTML code portion of the page for the most basic CRUD operations

<fieldset> <legend> test Web Api </legend> <a href= "Javascript:add ()" > Add (POST) </a> & Lt;a href= "javascript:update (1)" > update (Put) </a> <a href= "javascript:deletes (1)" > Delete (delete) </a> & Lt;a href= "/api/test" > List (Get) </a> <a href= "/API/TEST/1" > Entity (GET) </a></fieldset>< script> function Add () {$.ajax ({url: "/api/test/", type: "POST", Da TA: {"UserID": 4, "UserName": "Test", "UserEmail": "[email protected]"}, success:function (data) {alert (JSO N.stringify (data));    }           });            }//Update function 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>

The following is the code for the Apicontroller section:

   <summary>//Test module API///uri:/api/test//</summary> public class Testcontroller:apic Ontroller {//<summary>//User Data List///</summary> private readonly Li st<users> _userlist = new List<users> {new Users {UserID = 1, UserName = "Zzl", UserEmail = "           [email protected] "}, new Users {UserID = 2, UserName =" Spiderman ", UserEmail =" [email protected] "},        New Users {UserID = 3, UserName = "Batman", UserEmail = "[Email protected]"}};  <summary>//Get list objects///</summary>//<returns></returns> public        Ienumerable<users> Get () {return _userlist; }///<summary>//Get an entity, according to the primary key////</summary>//<param name= "id" ></par  am>//<returns></returns> public Users Get (int id)      {return _userlist.firstordefault (i = I.userid = = ID); }///<summary>//Add///</summary>//<param name= "Form" > Form object, it is unique </            param>//<returns></returns> Public users Post ([Frombody] users entity) {            _userlist.add (entity);        return entity;        }//<summary>//Update//</summary>/<param name= "id" > Primary key </param> <param name= "Form" > Form object, which is unique </param>//<returns></returns> public User            s Put (int id, [frombody]users entity) {var user = _userlist.firstordefault (i = I.userid = = ID); if (user! = null) {user. UserName = entity.                UserName; User. UserEmail = entity.            UserEmail;        } return user; }//<summary>//delete//</summary>        <param name= "id" > Primary key </param>//<returns></returns> public void Delete (int id)        {_userlist.remove (_userlist.firstordefault (I=>i.userid==id)); }    }

Let's look at the results of each action call:

Add operation (POST)

Update action (PUT)

Finally, again, the Web API uses a restful architecture, unlike the traditional (and soap) RPC Way of WCF and Web Service, which emphasizes the concept of interface modules, each module is independent, each module interface method is unified and single, That is, the Crud method is composed.

RESTful standards-based web API

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.