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.