ASP. net mvc Web API Post FromBody (how to correctly Post a Web API)

Source: Internet
Author: User

ASP. net mvc Web API Post FromBody (how to correctly Post a Web API)
Problem scenario: ASP. net mvc Web API defines the Post method, HttpClient uses JsonConvert. serializeObject is called by passing parameters. For example, the AddProduct method is defined in the Web Api. The parameter is the information (such as id and name) of the Product, and then the processing information is returned after the operation. Problem Analysis: I wrote a Web API blog post titled ASP. NET Web API/mvc api (with Demo), but only explains the Get usage, because it is relatively simple, parameters can be transmitted through URL, such as URL: http://localhost:9283/api/product/get/1 This indicates that the Product with ID 1 is obtained in the Controller Product. The client does not need to configure anything. You can directly input this URL in HttpClient. For how to use Post in Web APIs, refer to HttpClient + ASP. NET Web API, another option other than WCF, I tried according to the method in the article, the Code is as follows: ProductController code: public class ProductController: apiController {[HttpPost] public int AddProduct (string id, string name) {return 1 ;}} HttpClient Test Call code: [Fact] public void WebApiTest_AddProduct () {using (var client = new HttpClient () {client. baseAddress = new Uri (" http://localhost:1661/ "); Var requestJson = JsonConvert. serializeObject (new {id = "1", name = "2"}); HttpContent httpContent = new StringContent (requestJson); httpContent. headers. contentType = new MediaTypeHeaderValue ("application/json"); var result = client. postAsync ("api/Product/AddProduct", httpContent ). result. content. readAsStringAsync (). result ;}} MapHttpRoute route configuration: public static void Register (HttpConfiguration c Onfig) {config. routes. mapHttpRoute (name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new {id = RouteParameter. optional}) ;}it seems that there is no problem, but why is the action that matches the request not found on the controller "Product." What about errors? From the exception information, we can see that it should be a routing problem, that is, the Action-AddProduct cannot be found, but the write route configuration is no problem (I think), and then search for various keywords on the Internet, after finding a lot of information, I also tried many ways, because I am not very familiar with the web api routing configuration, and then I created a Demo and tested it at and found several points, for example, the default route configuration does not include {action}, and there is PostAsync in the test code above. The default configuration does not know why it is used as Get. For example, we remove a name parameter from the AddProduct method, at this time, we will find that AddProduct is executed, but the parameter id is changed to "AddProduct". Obviously, the action name is treated as the id value. I think there are two reasons for this, one is that my route configuration is not well configured, and there is a problem with the code writing on the test client, because I am eager to solve this problem, so I have not studied the causes, fromBody (only one I used it when I was looking for a problem, but it was not successful at the time. Now I forget how the code was written. Record how Post can run. ProductController code: public class ProductController: ApiController {[HttpPost] public int AddProduct ([FromBody] Product product) {return 1 ;}public class Product {public string id {get; set ;} public string name {get; set ;}}}

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.