Since our entire Web API platform is based on MVC based on API development, the entire Web API interface, when defined, generally needs to be displayed to declare that the interface is [HttpGet] or [HttpPost], although some interfaces may not be used. Declare, but avoid the following error message, explicit declaration is still good. The requested resource does not support the http method "POST" For example, the lookup object interface defined in the base class is as follows. /// "summary" /// Query the database to check if there is an object with the specified ID /// "/summary" /// The ID value of the "param name="id" object is "/param" /// "returns" exists to return the specified object, otherwise return Null "/returns" [HttpGet] Public virtual T FindByID(string id, string token) If the interface is added or deleted, it is generally required to submit data for the POST method, and it is necessary to carry more parameters based on security considerations. /// "summary" /// Insert the specified object into the database /// "/summary" /// "param name="info"" specified object "/param" /// "Returns" is successful. /returns [HttpPost] Public virtual CommonResult Insert(T info, string token, string signature, string TImestamp, string nonce, string appid) In the general Web API interface, we may encounter many simple types of parameters, but want them to submit data in POST, then we can have two ways to deal with, one is to define a class to place these Parameters, one is to use dynamic JObject parameters, the former has a lot of inconvenient, because we can not define more than one entity class for each interface parameter, so there may be many difficult to manage class definitions. As shown below, the WeChat API call interface case, we also need to set such a processing rule. Interface call request description Http request method: POST (please use https protocol) Https://api.weixin.qq.com/cgi-bin/groups/update? Access_token=ACCESS_TOKEN POST data format: json POST data example: {"group": {"id": 108, "name": "test2_modify2"}} So we use JObject like this, let's look at the interface definition and processing code. JObject is an object under the Newtonsoft.Json.Linq namespace. /// "summary" /// Modify user password /// "/summary" /// "param name="param"" contains the composite object userName and userPassword "/param" /// "param name="token"" user access token "/param" /// "returns" /returns [HttpPost] Public CommonResult ModifyPassword(JObject param, string token) { //Token check, throw an exception if it doesn't pass CheckResult checkResult = CheckToken(token); Dynamic obj = param; If (obj != null) { String userName = obj.userName; String userPassword = obj.userPassword; Bool success = BLLFactory "User".Instance.ModifyPassword(userName, userPassword); Return new CommonResult(success); } Else { Throw new MyApiExcepTIon ("pass parameter error"); } } When we convert the JObject object to the object we need, because we have not defined a concrete entity class, we use the dynamic syntax to declare that this is a dynamic object, and the corresponding property is obtained by the runtime. Dynamic obj = param; In this way, we can dynamically link the corresponding JSON object to the Web API interface when calling, without pre-defining the classes of various interface parameters. /// "summary" /// Call the Web API interface to change the user password /// "/summary" /// "param name="userName"" user name "/param" /// "param name="userPassword" modified password "/param" /// "returns" returns true if the modification succeeds, otherwise returns false "/returns" Public bool ModifyPassword(string userName, string userPassword) { Var acTIon = "ModifyPassword"; Var postData = new { userName = userName, userPassword = userPassword }.ToJson(); String url = GetTokenUrl(acTIon); CommonResult result = JsonHelper "CommonResult".ConvertJson(url, postData); Return (result != null) ? result.Success : false; } The GetTokenUrl is a complete commit address based on parameters such as the address of the token and the API. We passed the code above Var postData = new { userName = userName, userPassword = userPassword }.ToJson(); You can dynamically create an object, and generate its JSON string, submit the data POST to the corresponding API interface, and then convert the result to the object is completed. All in one pc 23.8 inch, all in one pc i5 8gb ram, all in one desktop intel core i5, pc all in one 24 inch Guangdong Elieken Electronic Technology Co.,Ltd. , https://www.elieken.com
Web API interface design experience summary 1, determine the MVC GET or POST mode in the interface definition