如何调用ASP.NET Web API删除方法接受复杂对象作为参数 [英] How to call an ASP.NET Web API Deleting Method accept a complex object as parameter

查看:113
本文介绍了如何调用ASP.NET Web API删除方法接受复杂对象作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Web API(DELETE):

I have the following Web API (DELETE):

public class UsersController : ApiController
{
          public HttpResponseMessage Delete(FileCloud objFile)
        {
            using (FileCloud obj = new FileCloud())
            {
                return new HttpResponseMessage()
                {
                   //Some code
                };
            }
        }
}



其中CustomerData


where CustomerData

public class CustomerData
{
    public string FirstName {get; set;}
    public string LastName {get; set;}
    public DateTime BirthDate {get; set;}
}



如何在Windows Form Application中调用WEB API服务我试过使用下面的代码但不起作用,因为删除方法不接受仅对象参数URI


how to call WEB API Service inside Windows Form Application i tried to use the Below Code but didn't work because delete method doesn't accept object parameter only URI

HttpResponseMessage Result = new HttpResponseMessage();
       var objFileCloud = new Dictionary<string,>() {
               { "FirstName ", "var1"},
               { "LastName ", "var2"  }
               };
       using (var client = new HttpClient())
       {
           var content = new FormUrlEncodedContent(objFileCloud);
           Result = client.DeleteAsync(URI, content).Result;
       }

推荐答案

参考 - HTTP操作从.NET客户端获取,POST,PUT和DELETE [ ^ ]。



阅读放置和删除请求方法部分。

API



Refer - HTTP Operations GET, POST, PUT and DELETE From .NET Client[^].

Read the section "Put and Delete Request Method".

API


namespace WebAPI.Controllers
{
    public class person
    {
        public string name { get; set; }
        public string surname { get; set; }        }
    public class personController : ApiController
    {
        [HttpPut]
        public void Put([FromBody] person p)
        {
        }
    }
}



客户端应用程序




Client App


namespace ConsoleAPP
{
    public class person
    {
        public string name { get; set; }
        public string surname { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            using (var client = new HttpClient())
            {
                person p = new person { name = "Sourav", surname = "Kayal" };
                client.BaseAddress = new Uri("http://localhost:1565/");
                var response = client.PutAsJsonAsync("api/person", p).Result;
                if (response.IsSuccessStatusCode)
                {
                    Console.Write("Success");
                }
                else
                    Console.Write("Error");
            }
        }
    }
}


这篇关于如何调用ASP.NET Web API删除方法接受复杂对象作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆