对于删除不存在的对象的请求应该返回什么状态码?
public ContentResult DeleteEntity(int id, FormCollection FormData)
{
Database db = new Database();
TargetEntity te = db.TargetEntities.SingleOrDefault(t => t.Id == id);
if(te == null)
{
Reponse.StatusCode = 400; //Is this correct?
return Content("Deletion failed. Invalid ID: " + id);
}
//Delete the entity
return Content("Successfully Deleted");
}
请求本身没有问题,只是碰巧指定的 ID 无效(或者该项目已被删除),所以我不确定 400 范围。我敢肯定 500 代码更不适合这种情况,因为服务器上没有任何问题(只是要求删除不存在的内容)。
什么状态码在这里最合适?
最佳答案
What status code should be returned for a request to delete an object that doesn't exist?
关于http - 给定无效 ID 时应返回什么 HTTP 状态代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16469570/