WebAPI post get, webapipostget

Source: Internet
Author: User

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.

 

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.