Link to home
Start Free TrialLog in
Avatar of wael_shehab
wael_shehabFlag for Saudi Arabia

asked on

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;
                }
ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wael_shehab

ASKER

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
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.
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
thank you it is working now