Webapi Parameter Delivery Summary

Source: Internet
Author: User

In Webapi, the request Principal (Httpcontent) can only be read once, not cached, and only the stream is read forward.

Give examples:

1. Request Address:/?id=123&name=bob

Server-side method: void Action (int id, string name)//All parameters are simple types and will therefore come from the URL

2. Request Address:/?id=123&name=bob

Service-side method: void Action ([fromuri] int ID, [Fromuri] string name)//IBID.

void Action ([frombody] string name); [Formbody] attribute display indicates that the entire body is read as a string parameter

3. Request Address:/?id=123

Class definition:

public class Customer {//A complex object type defined by
public string Name {get; set;}
public int Age {get; set;}
}

Server-side method: void Action (int id, Customer c)//parameter ID read from query string, parameter C is a complex Customer object class play, read from body through formatter

Service-side method: void Action (Customer c1, customer C2)//Error! Multiple parameters are complex types that try to read from the body and the body can only be read once

Service-side method: void Action ([Fromuri] customer C1, customer C2)//YES! Unlike the action above, the complex type C1 will be read from the URL and C2 will be read from the body

4.ModelBinder mode:

void Action ([Modelbinder (Mycustombinder)] SomeType c)//marking using a specific model binder to parse parameters

[Modelbinder (Mycustombinder)] public class SomeType {}//By labeling a specific type of SomeType [Modelbidner (Mycustombinder)] Attribute enables all SomeType type parameters to apply this rule

void Action (SomeType c)//Because the type of C is SomeType, the properties on the SomeType are used to determine the model binding

Summarize:

1. The default simple parameters are passed through URL parameters, with the exception of:

1.1 If the ID parameter is included in the route, the ID parameter is passed by route;

1.2 If the parameter is marked as [Frombody], it can be a simple parameter that the client passes via post: $.ajax (URL, ' =value '), or $.ajax ({url:url, data: {': ' Value '}});

2. The default complex parameters (custom entity classes) are delivered via post, with the exception of:

2.1 If the parameter value is marked as [Fromuri], then the argument can be a complex parameter;

3. Parameters marked as [Frombody] are allowed only once, and parameters marked as [Fromuri] can occur multiple times, and if the parameter labeled [Fromuri] is a simple parameter, the tag can be removed.

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.