The main purpose of WEBAPI is to pass "specified parameters" into "API backend", API receive parameters, "corresponding business logic processing", "return results". So how to pass the argument, or the popular saying, the HTTP request should be how to request API,API background how to write, in order to accurately receive parameters.
HttpGet Request
1. Get request, single parameter
Front-End Ajax
var url = ' Api/enterorexit/test ';
var para = {};
para["Phone"] = "phone13880825221";
para["UID"] = "uid287572292";
$.get (URL, para, function () {}, "Application/json");
Back end
[HttpGet]
Public Ihttpactionresult GetData2 (string Phone)
{
string result = "interface has gone through";
return ok<string> (Result);
}
After debugging, the back-end phone= "phone13880825221", the success of the transfer of parameters.
**********************************************************************************
2, GET request, pass multiple parameters
Front-End Ajax
var url = ' Api/enterorexit/test ';
var para = {};
para["Phone"] = "phone13880825221";
para["UID"] = "uid287572292";
$.get (URL, para, function () {}, "Application/json");
Back end
[HttpGet]
Public Ihttpactionresult GetData2 (string phone,string UID)
{
string result = "interface has gone through";
return ok<string> (Result);
}
After commissioning, the backend phone= "phone13880825221", uid= "uid287572292"; The success of the pass.
PS, back-end parameter name, whether you are writing phone,phone,uid,uid,uid, you can receive the front-end Ajax request passed parameters, this is ignored case.
**********************************************************************************
3. Get request, transfer entity
Front-End Ajax
var url = ' Api/enterorexit/test ';
var para = {};
para["Phone"] = "phone13880825221";
para["UID"] = "uid287572292";
$.get (URL, para, function () {}, "Application/json");
Back end
[HttpGet]
Public Ihttpactionresult GetData2 (Requestmodel model)
{
String msg = "";
String code = "";
string result = "interface has gone through";
return ok<string> (Result);
}
After commissioning, Requestmodel =null, Nani? What happened? Come on, Firefox grab the bag, look at it,
When the GET request, the default is to put all the parameters in the URL directly in the form of a string, the background is naturally not connected. !! String form, oh, isn't it easy to think of a train of thought? It is also a workaround to serialize the JSON object into a JSON string, receive the JSON format string in the background, and deserialize it into an entity.
Front-End Ajax
var url = ' Api/enterorexit/test ';
var para = {};
para["Phone"] = "phone13880825221";
para["UID"] = "uid287572292";
var requeststr=json.stringify (para);
$.get (URL, requeststr, function () {}, "Application/json");
Back end
[HttpGet]
Public Ihttpactionresult GetData2 (string requeststr)
{
var model= newtonsoft.json.jsonconvert.deserializeobject<tb_requestmodel> (REQUESTSTR);
String msg = "";
String code = "";
string result = "interface has gone through";
return ok<string> (Result);
}
This is a way, of course, there is a better way, according to the Big God in the garden of the blog, know get request when you can add [Fromuri] in the parameters to directly get the object.
Front-End Ajax
var url = ' Api/enterorexit/test ';
var para = {};
para["Phone"] = "phone13880825221";
para["UID"] = "uid287572292";
$.get (URL, para, function () {}, "Application/json");
Back end
[HttpGet]
Public Ihttpactionresult test ([Fromuri]requestmodel model)
{
String msg = "";
String code = "";
string result = "interface has gone through";
return ok<string> (Result);
}
After testing, the model is not empty and can get the value.
(ii) pits in the ASP. NET Web API-"parameters in an HTTP GET request"