Some days ago, Insus.net had an operation that demonstrated the Web API interface:
"How to operate the Webapi interface (display data)" http://www.cnblogs.com/insus/p/5670401.html
"ASP. Webapi interface operation (add, update and delete)" http://www.cnblogs.com/insus/p/5673641.html
However, some netizens say, do not want to use jquery, all with ASP. NET MVC to achieve. Ok, let's see, first, to implement the Post function, which is to add data to the Web API.
In the controller, create two action, which is the view action, and the other is the post action:
New HttpClient (); New " Application/json " ); Client. Postasync ("http://localhost:9001/api/size", httpcontent) = > { postTask.Result.EnsureSuccessStatusCode (); });
Source Code
View Code:
Run the program to see if it works properly, does the database have data to insert?
The following shows the update operation put, in the controller, creating an action, one displaying the data, and the other updating the data:
PublicActionResult Putdemo (intID) {varSizes = apiutility.get<size> ("http://localhost:9001/api/size/"+ID); varModel =sizes. FirstOrDefault (); returnView (model); } [HttpPost] Publicactionresult Putdemo (size size) {HttpClient client=NewHttpClient (); Httpcontent httpcontent=NewStringcontent (size. ToJson (), System.Text.Encoding.UTF8,"Application/json"); Client. Putasync ("http://localhost:9001/api/size", Httpcontent). ContinueWith ((posttask)={postTask.Result.EnsureSuccessStatusCode (); }); returnRedirecttoaction ("Putdemo", size. SIZE_NBR); }
Source Code
View:
Live Demo:
The following insus.net again the function of Delete, in implementing the removal of this function, a little bit of difficulty:
First, create an action on the controller:
PublicActionResult Deletedemo (intID) {varSizes = apiutility.get<size> ("http://localhost:9001/api/size/"+ID); varModel =sizes. FirstOrDefault (); returnView (model); } [HttpPost] Publicactionresult Deletedemo (size size) {HttpClient client=NewHttpClient (); Client. Deleteasync ("http://localhost:9001/api/size/"+size. SIZE_NBR). ContinueWith ((posttask)={postTask.Result.EnsureSuccessStatusCode (); }); returnRedirecttoaction ("ShowApiData1"); }
Source Code
See no, in the tag # # #, the Deleteasync method, which is not accepting a complex complex pair of type. In order not to make too many changes to the previous days ' programs, Insus.net had to modify the Web API. This may be a lack of thought when designing Web APIs.
Add one more method overload for the API interface:
This web API is from the Create and use Web API http://www.cnblogs.com/insus/p/5019088.html
Add Delete view deletedemo.cshtml:
Delete Feature Live Demo:
Post,put,delete data for Web API interfaces without using jquery