我正在尝试使用web api,其中操作方法名称就像CreateCustomer(..),GetCustomer(...)一样。这些是用[HttpPost] / [HttpGet]注释的。我不知道如何使用HttpClient()消耗它。如果设置如下,我打电话
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://host/directory/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = client.PostAsJsonAsync("api/customer", cutomerObj).Result;
它会抛出404.如果我使用fiddler并使用JSON客户对象向http://host/directory/api/customer发送isquest它完美无缺
我到底错过了什么?我需要使用POST和GET作为方法吗?
答案 0 :(得分:1)
您需要将路线配置为包含action
api/{controller}/{action}/{id}
,并调用api/customer/CreateCustomer
来自C#,
var t = new HttpClient().GetAsync("http://localhost:63154/api/UserApi/CreateCustomer").Result.Content.ReadAsStringAsync().Result;