A week ago the company used WEBAPI to deal with some things, to summarize their usage.
1. Creating an empty Webapi will default to a few methods.
Public classValuecontroller:apicontroller {//GET Api/value Publicienumerable<string>Get () {return New string[] {"value1","value2" }; } //GET API/VALUE/5 Public stringGet (intID) {return "value"; } //POST Api/value Public voidPost ([Frombody]stringvalue) { } //PUT API/VALUE/5 Public voidPut (intID, [frombody]stringvalue) { } //DELETE API/VALUE/5 Public voidDelete (intID) {}}
First ienumerable<string> Get ()
$.ajax ({ "/api/value", "Get", function ( Data) { alert (data); } });
Second string Get (int ID)
$.ajax ({ "/api/value/1", "Get", function (data) { alert (data); } });
The third void Post ([frombody]string value) is special when called, and I can transfer the value to the background when the key value in front of the data is empty.
$.ajax ({ "/api/value", "POST" , "": "Test" }, function (data) { //alert (data); } });
Or to transform the background receive method. The entity is used to receive the value, which can be received.
public class Postmodel { public string
value {get ; set public string Post ([Frombody]postmodel value) { return " ok "
$.ajax ({ "/api/value", "POST" , ' value ': "Test" }, function (data) { //alert (data); } });
When the fourth string Put (int id, [Frombody]postmodel value) is called, the ID must be included in the path before it can be obtained. It is useless to put it in the receiving entity.
$.ajax ({ "/api/value/1", "Put" , ' value ': "Test" }, function (data) { //alert (data); } });
The fifth string Delete (int id) call differs from the first one by stating the type: "Delete"
This is the default number of ways to use, but when you use it, you feel a lot of constraints. Then I deleted them all.
Usage is not standard, but used to feel more comfortable, feel the code or how to write comfortable how to come, oneself in the API route here add a {action} so use to look like MVC is not very different.
Public Static classWebapiconfig { Public Static voidRegister (httpconfiguration config) {config. Routes.maphttproute (Name:"Defaultapi", Routetemplate:"Api/{controller}/{action}/{id}", defaults:New{id =routeparameter.optional}); } }
The code can be written like this.
Public classValuecontroller:apicontroller { Publiclist<string>GetList () {return Newlist<string> {"value1","value2" }; } Publiclist<string>gettestlist () {return Newlist<string> {"value1","value2" }; }} $.ajax ({URL:"/api/value/gettestlist", type: "Post", Success:function(data) {}});
For WEBAPI parameters, the default is to extract the arguments from the path, which is the Get method.
Public list<string> GetList (string id,string name) { returnnew List <String> {"value1", "value2" }; }
$.ajax ({ URL: "/api/value/getlist", type: "Get",
Data: {"id": "1", "name": "Three"}, function (data) { } });
$.ajax ({ URL: "/api/value/getlist? id=1&name= three ", type: "Get", function (data) { } });
If you need to pass a lot of string parameters, get method will be error, you can use post, it is important to note that you cannot start with get, or it only recognize the get commit
Public classPostmodel { Public stringValue {Get;Set; } Public stringid{Get;Set; } } Publiclist<string>List ([Frombody]postmodel value) {return Newlist<string> {"value1","value2" }; }
$.ajax ({ "/api/value/list", "POST", "id": "1", "Value": " 01234567890123456789012345678901234567890123456789 " }, function (data) { } } );