wael_shehab
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(objF ileCloud);
Result = client.DeleteAsync(URI, content).Result;
}
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(objF
Result = client.DeleteAsync(URI, content).Result;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
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
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
ASKER
thank you it is working now
ASKER
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.DefaultRequestHeade
client.DefaultRequestHeade
// HTTP POST
FileCloud obj= new FileCloud() ;
var response = await client.PostAsJsonAsync("/D
}
but it give me this message:
Method: POST, RequestUri: 'http://localhost:20566/Delete', Version: 1.1, Content: System.Net.Http.ObjectCont
by Another Word it did'nt implement the webAPI method Delete
So What Can i Do
Thanks