ASP. NET Webapi Parameter binding summary

Source: Internet
Author: User

The first thing you have to do is to correct the misconception that you have always understood about get requests and post requests: A GET request can only be passed through a URL, and the POST request can only be passed through the body.

In fact, the above understanding is wrong, read a lot of information and concrete practice, the correct understanding should be: Get and post is the HTTP protocol (specification) defined and the different methods of server interaction, get used to obtain resources from the server (security and power, etc.), post is used to modify the resources on the server. There is no relationship between the method and the request, and the Get and post requests can either accept URL arguments or receive body arguments, depending on the parameter binding mechanism of the server.

OK, back to the topic, WEBAPI parameter binding best practices, directly on the example:

1: Simple type parameter binding &url pass parameter:

Service side:

[HttpGet]
[HttpPost]
Public Httpresponsemessage finntest (String Name,int age)
{
var json = "{name: '" + name + "', Age:" + Age + "}";
var resp = new Httpresponsemessage
{
Content = new Stringcontent (JSON, System.Text.Encoding.UTF8, "Application/json")
};
Return resp;
}

Client Invoke Call:

Post http://localhost:27984/HjkDealer/FinnTest?name=finn&age=88 http/1.1

2: Simple type parameter binding &body pass parameter

Service side:

[HttpGet]
[HttpPost]
Public Httpresponsemessage FinnTest2 ([frombody]string name, Int. age)
{
var json = "{name: '" + name + "', Age:" + Age + "}";
var resp = new Httpresponsemessage
{
Content = new Stringcontent (JSON, System.Text.Encoding.UTF8, "Application/json")
};
Return resp;
}

Client calls:

Post http://localhost:27984/HjkDealer/FinnTest2?age=88 http/1.1
Content-type:application/x-www-form-urlencoded;charset=utf-8

=finn

Note the red callout: content-type head must, otherwise the name parameter cannot receive correctly, the body body writes: "Name=finn" or "Finn", the name parameter also cannot receive correctly, the deep reason is unclear

3: Complex type parameter binding &body argument (complex type default body pass parameter)

public class Fiona
{
public string name {get; set;}

public string Age {get; set;}
}

[HttpGet]
[HttpPost]
Public Httpresponsemessage FinnTest4 (Fiona Fiona)
{
var json = "{name: '" + fiona.name + "', Age:" + Fiona.age + "}";
var resp = new Httpresponsemessage
{
Content = new Stringcontent (JSON, System.Text.Encoding.UTF8, "Application/json")
};
Return resp;
}

Client calls:

Post Http://localhost:27984/HjkDealer/FinnTest4 http/1.1
Content-type:application/x-www-form-urlencoded;charset=utf-8

name=finn&age=88

4:list the JSON method of the complex type parameter binding &body (complex type default body pass parameter)

Service side:

[HttpGet]
[HttpPost]
Public httpresponsemessage FinnTest5 (list<fiona> fi)
{
var json = "{name: '" + fi[0].name + "', Age:" + Fi[0].age + "}";
var resp = new Httpresponsemessage
{
Content = new Stringcontent (JSON, System.Text.Encoding.UTF8, "Application/json")
};
Return resp;
}

Client calls:

Post Http://localhost:27984/HjkDealer/FinnTest5 http/1.1
Content-type:application/json

[{name: "Finn", Age:88},{name: "Fiona", age:99}]

ASP. NET Webapi Parameter binding summary

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.