Click here to Skip to main content
15,885,878 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following Web API (DELETE):
C#
public class UsersController : ApiController
{
          public HttpResponseMessage Delete(FileCloud objFile)
        {
            using (FileCloud obj = new FileCloud())
            {
                return new HttpResponseMessage()
                {
                   //Some code
                };
            }
        }
}

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

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
C#
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;
       }
Posted
Updated 16-Apr-15 4:12am
v3

1 solution

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

Read the section "Put and Delete Request Method".

API


C#
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


C#
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");
            }
        }
    }
}
 
Share this answer
 
Comments
Member 11593359 18-Apr-15 7:11am    
thank you so much for your reply
there is no problem in my post method i have aproblem in delete method because delete method doesn't accept object parameter only URI
Result = client.DeleteAsync(URI)
In case of delete, you can need a key to delete, that's it. Else, you can write a POST method and send the object and do whatever you want to do inside that.
Member 11593359 22-Apr-15 4:47am    
thank you it is working now
That's cool. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900