我有一个带有控制器的 Web API 服务,如下所示。
RoutePrefix("api/Product")]
public class ProductController : ApiController
{
[HttpPost]
[POST("InsertProduct")]
public HttpResponseMessage InsertProduct(ProductDetail product)
{
//logic
}
[HttpGet]
[GET("Item/{itemId}/{itemtypeid}")]
public List<ProductDetail> GetProduct(int itemId, long itemtypeid)
{
//logic
return response;
}
}
这里的GET请求来自Postman 工具(chrome Rest 服务测试器 url)和客户端(参见下文)。POST请求可与 Postman 工具一起正常工作。但是当我从客户端尝试相同的代码时,我收到以下错误“ StatusCode:404,ReasonPhrase:'Not Found' ”。
客户端代码:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost/ProductService/");
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
string serializedContent = JsonConvert.SerializeObject(new ProductDetail());
StringContent stringContent = new System.Net.Http.StringContent(serializedContent, Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PostAsJsonAsync<ProductDetail>("api/Product/InsertProduct", new ProductDetail()).Result;
相同的请求如何从任何外部工具工作,但不能从 httpclient 工作。
请帮我解决一下这个。