zoukankan      html  css  js  c++  java
  • 不使用jQuery对Web API接口POST,PUT,DELETE数据

    前些天,Insus.NET有演示Web API接口的操作:
    怎样操作WebAPI接口(显示数据)http://www.cnblogs.com/insus/p/5670401.html

    ASP.NET MVC对WebAPI接口操作(添加,更新和删除)http://www.cnblogs.com/insus/p/5673641.html

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

    在控制器中,创建两个Action,即是视图Action,另一个是POST的Action:


    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

    视图代码:

    程序运行,看看是否能正常运行,数据库是否有数据插入?

    下面演示更新操作PUT,在控制器中,创建Action,一个显示数据,另一个更新数据:


     public ActionResult PutDemo(int id)
            {
                var sizes = ApiUtility.Get<Size>("http://localhost:9001/api/size/" + id);
                var model = sizes.FirstOrDefault();
                return View(model);
            }
    
            [HttpPost]
            public ActionResult PutDemo(Size size)
            {
                HttpClient client = new HttpClient();
    
                HttpContent httpcontent = new StringContent(size.ToJson(), System.Text.Encoding.UTF8, "application/json");
    
                client.PutAsync("http://localhost:9001/api/size", httpcontent)
                    .ContinueWith((postTask) =>
                    {
                        postTask.Result.EnsureSuccessStatusCode();
                    });
                return RedirectToAction("PutDemo", size.Size_nbr);
            }
    Source Code


    视图:

     
    实时演示:

     下面Insus.NET再把Delete的功能实现完,在实现删除这个功能时,出现一点点困难:
    先在控制器创建操作Action:

    public ActionResult DeleteDemo(int id)
            {
                var sizes = ApiUtility.Get<Size>("http://localhost:9001/api/size/" + id);
                var model = sizes.FirstOrDefault();
                return View(model);
            }
    
            [HttpPost]
            public ActionResult DeleteDemo(Size size)
            {
                HttpClient client = new HttpClient();
    
                client.DeleteAsync("http://localhost:9001/api/size/" + size.Size_nbr)
                    .ContinueWith((postTask) =>
                    {
                        postTask.Result.EnsureSuccessStatusCode();
                    });
                return RedirectToAction("ShowApiData1");
            }
    Source Code

    看到否,在上图中的标记#2中,DeleteAsync方法,它是不接受一个复杂Complex对type。为了不需要对前些天的程序作过多的修改,因此Insus.NET只得对Web API进行修改。这也许是设计Web API时欠缺的思考。

    为API接口添加多一个方法重载:


    这个Web API来自《创建与使用Web APIhttp://www.cnblogs.com/insus/p/5019088.html

     
    添加删除视图DeleteDemo.cshtml:

    删除功能实时演示:



  • 相关阅读:
    智能指针之 auto_ptr
    UML在线绘图
    inline使用
    工作随笔—2017-12-12
    链表排序
    转——浅谈如何提高服务器并发处理能力
    使用re开发python计算器
    Linux-centos7下python3 环境设置
    C语言中的static 详细分析
    pycharm import pygame 出现报错:No module named 'pygame'
  • 原文地址:https://www.cnblogs.com/insus/p/5684577.html
Copyright © 2011-2022 走看看