(ii) pits in the ASP. NET Web API-"parameters in an HTTP GET request"

Source: Internet
Author: User

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.

——————————————————————————————————————————————————————

Knowledge Point: The data from the GET request in the HTTP protocol is appended to the URL (that is, the data is placed in the HTTP protocol header), and the post request is placed in the package body of the HTTP protocol package.

(ii) pits in the ASP. NET Web API-"parameters in an HTTP GET request"

Related Article

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.