使用自定义名称的 Web API haing 操作方法

Consume Web API haing Action methods with custom name

我正在尝试使用 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 发送请求一样完美

我到底错过了什么?我需要使用 POST 和 GET 作为方法吗?

您需要配置您的路由以包含 actionapi/{controller}/{action}/{id} 并拨打 api/customer/CreateCustomer

来自 C#,

var t = new HttpClient().GetAsync("http://localhost:63154/api/UserApi/CreateCustomer").Result.Content.ReadAsStringAsync().Result;