今晚我遇到了一个奇怪的问题。我尝试在我的REST API中实现POST方法,但是调用了GET。我还可以看到301与Fiddler永久移动。
这是我的代码,我用Postman测试过。当我得到它时,它还调用GET方法:
[Route("api/v1/shops/{shopId}/products/")]
[HttpGet]
public HttpResponseMessage GetProducts(int shopId)
{
...
return Request.CreateResponse(HttpStatusCode.OK);
}
[Route("api/v1/shops/{shopId}/products/")]
[HttpPost]
public HttpResponseMessage CreateProduct(int shopId, Product product)
{
...
return Request.CreateResponse(HttpStatusCode.OK);
}
我尝试使用不同的方法签名,使用硬编码的ID,但失败了:
[Route("api/v1/shops/1/products/")]
[HttpPost]
public HttpResponseMessage CreateProduct(Product product)
更新
我也有在今晚之前工作的方法......
[Route("api/v1/shops/")]
[HttpPost]
public HttpResponseMessage CreateShop(Shop shop)