WebAPI post get, webapipostget
In short, when the GET method is used in the WEBAPI, when the parameter is accepted, the [fromuri] annotation is stated before the parameter to be obtained from the uri, for example:
[HttpPost] public IHttpActionResult AddProductToCart_Details ([fromuri] int productId, [fromuri] int shoppingCartTypeId, [fromuri] int quantity, [fromuri] Loginstatus login) {.......................} model namespace Nop. plugin. misc. webApiServices. models {public class LoginStatus {public int userid {set; get;} public string logguid {set; get ;}}}
Test url:
Http: // 192.168.1.127: 8089/api/shoppingcart/AddToCart? ProductId = 59 & shoppingCartTypeId = 1 & quantity = 10 & userid = 1 & logguid = 729b37c4-63e1-4961-9615-dcf9f314a814
Yes,
When I changed to post, I changed [httpget] to [httppst] And then changed the parameter acceptance to frombody. However, if an error occurs, it indicates that the parameter cannot be bound, after checking the information, we found that in addition to the descriptions, we can only enable frombody annotation for one parameter, and then re-encapsulate the parameter.
[HttpPost] public IHttpActionResult AddProductToCart_Details ([FromBody] AddToCart add ){......} model namespace Nop. plugin. misc. webApiServices. models {public class LoginStatus {public int userid {set; get;} public string logguid {set; get ;}} public class AddToCart {private LoginStatus _ login; public int productId {set; get;} public int shoppingCartTypeId {set; get;} public int quantity {set; get;} public LoginStatus login {set {_ login = value ;} get {return _ login ;}}}
Form used to test post
<body> <form method="post" action="http://localhost:15536/api/shoppingcart/AddToCart"> <input type="text" name="productid" value="59"> <input type="text" name="shoppingcarttypeid" value="1" > <input type="text" name="quantity" value="1"> <input type="text" name="login.userid" value="1"> <input type="text" name="login.logguid" value="729b37c4-63e1-4961-9615-dcf9f314a814" > <input type="submit" /> </form></body>
The only note is that the name is marked as login. userid login. logguid when the parameter in login is passed. Otherwise, the binding cannot be performed.