【发布时间】:2019-07-28 12:21:09
【问题描述】:
在下面的 API 控制器中,如果在 API 上调用 POST 或 PUT,如何返回 405 Method Not allowed。目前它正在返回 404 not found 。
[HttpGet]
public ActionResult<IEnumerable<Customer>> Get()
{
string query = "select* from Customers;";
try
{
List<Customer> collection = GetCustomers(query);
return Ok(collection);
}
catch (System.Exception)
{
return BadRequest();
}
}
【问题讨论】:
-
你有一个
[HttpGet]属性。 .net core 会自动执行 405。 -
如果您现在尝试使用 Post 或 Put 调用端点会发生什么?我相信它给出了 405
-
返回 404 Not Found
-
GetCustomers定义在哪里? -
那你的路线错了。即它实际上 是 404。如果您使用 GET 以外的任何其他方式执行该实际操作,那么您将得到 405。
标签: c# asp.net-web-api asp.net-core