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 ;}}}