前些天,Insus.NET有演示Web API接口的操作:
http://www.cnblogs.com/insus/p/5670401.html

http://www.cnblogs.com/insus/p/5673641.html

但是,有网友说,不想使用jQuery,全部以ASP.NET MVC来实现。Ok,那来看看,先来实现POST的功能,即是往Web API添加数据。

在控制器中,创建两个Action,即是视图Action,另一个是POST的Action:
不使用jQuery对Web API接口POST,PUT,DELETE数据

HttpClient client = new HttpClient();

            HttpContent httpcontent = new StringContent(size.ToJson(), System.Text.Encoding.UTF8, "application/json");

            client.PostAsync("http://localhost:9001/api/size", httpcontent)
                .ContinueWith((postTask) =>
                {
                    postTask.Result.EnsureSuccessStatusCode();
                });
Source Code

相关文章: