Post,put,delete data for Web API interfaces without using jquery

Source: Internet
Author: User

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

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.