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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.