How to call an ASP.NET Web API Deleting Method accept a complex object as parameter

I have the following Web API (DELETE):

public class UsersController : ApiController
{
    public IEnumerable<Users> Delete(CustomerData Obj)
    {
         // Code
    }
}

where CustomerData
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

         HttpResponseMessage Result = new HttpResponseMessage();
                var objFileCloud = new Dictionary<string, string>() {
                        { "FirstName ", "var1"},
                        { "LastName ", "var2"  }
                        };
                using (var client = new HttpClient())
                {
                    var content = new FormUrlEncodedContent(objFileCloud);
                    Result = client.DeleteAsync(URI, content).Result;
                }
wael_shehabAsked:
Who is Participating?
 
Miguel OzSoftware EngineerCommented:
You could use PostAsJsonAsync method as explained in the section "Creating a Resource (HTTP POST)"

Let your URI be localhost:8080/Users/Delete, then the code looks like:

using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("localhost:8080/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        // HTTP POST
        var customerData= new CustomerData() { FirstName = "Foo", LastName= "var2" };
        response = await client.PostAsJsonAsync("Users/Delete", customerData);
        if (response.IsSuccessStatusCode)
        {
          //Handle response Content
        }
    }

Open in new window

0
 
wael_shehabAuthor Commented:
thank you for you reply,

but i tried to call webApi service Method

        public HttpResponseMessage Delete(FileCloud objFile)
        {
            using (FileCloud obj = new FileCloud())
            {
                return new HttpResponseMessage()
                {
                   //Some code
                };
            }
        }
using your code

         using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:20566/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                // HTTP POST
                 FileCloud obj= new FileCloud() ;
                 var response = await client.PostAsJsonAsync("/Delete", obj);
            }
but it give me this message:

Method: POST, RequestUri: 'http://localhost:20566/Delete', Version: 1.1, Content: System.Net.Http.ObjectContent`1[[WindowsFormsApplication.FileCloud, WindowsFormsApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
by Another Word it did'nt implement the webAPI method Delete

So What Can i Do
Thanks
0
 
Miguel OzSoftware EngineerCommented:
Uhmm. quick questions:
Q1. VS version and Web API version? My solution works for Web Api 2.0.
Q2. Is the controller method (Delete) a POST method? It seems that it is not from the original question post, please add  [HttpPost] attribute to your Delete method.
0
 
wael_shehabAuthor Commented:
Thank you for your reply,

about Q1: VS Version  is : Microsoft Visual Studio 11.0 and webApi Version is: Microsoft ASP.NET WEB API 2.2
About Q2 :yes i have already added    [HttpPost] attribute to my Delete method. but still not working
any idea why it did'nt work
thank you
0
 
wael_shehabAuthor Commented:
thank you it is working now
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.