I'm trying to pass a C# object to a web api controller. The api is configured to store objects of type Product that are posted to it. I have successfully added objects using Jquery Ajax method and now I'm trying to get the same result in C#.
I've created a simple Console application to send a Post request to the api:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public decimal Price { get; set; }
}
static void Main(string[] args)
{
string apiUrl = @"http://localhost:3393/api/products";
var client = new HttpClient();
client.PostAsJsonAsync<Product>(apiUrl, new Product() { Id = 2, Name = "Jeans", Price = 200, Category = "Clothing" });
}
The postproduct method is never invoked, how to send this object to the controller ?
Method used for adding items:
public HttpResponseMessage PostProduct([FromBody]Product item)
{
item = repository.Add(item);
var response = Request.CreateResponse<Product>(HttpStatusCode.Created, item);
string uri = Url.Link("DefaultApi", new { id = item.Id });
response.Headers.Location = new Uri(uri);
return response;
}