给出无效ID时应返回什么HTTP状态代码?

时间:2013-05-09 19:25:33

标签: http asp.net-mvc-2 http-headers http-error

对于删除不存在的对象的请求,应返回什么状态代码?

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个代码甚至不太适合这个,因为服务器上没有出错(只是要求删除不存在的东西)。

这里最合适的状态代码是什么?

1 个答案:

答案 0 :(得分:4)

  

对于删除不存在的对象的请求,应返回什么状态代码?

404 - Not Found

相关问题