Restful API - 当没有错误发生但未创建资源时 return 怎么办?

Restful API - What to return when no error occurred, but a resource wasn't created?

作为参考,我在处理此问题时参考了 http://www.restapitutorial.com/httpstatuscodes.html

我有一个 Web API 2.2 服务,允许用户使用 AddPrintRequest(...) 添加 "print requests",然后使用对 BatchRequests(...) 的调用来批处理这些请求。

BatchRequests 方法验证每个请求,并对有效请求进行批处理。如果至少有一个有效请求,则创建该批次,我们 return 一个 HTTP 201(已创建)。但是,如果没有创建批次,我们应该 return 怎么办?我们不认为这是一个错误,但想向客户发出信号,说明为什么没有创建批次。正确的状态代码是什么?

状态204 No Content可能适合这里。

您可以使用 Conflict,然后通过消息抛出异常。

Equivalent to HTTP status 409. Conflict indicates that the request could not be carried out because of a conflict on the server.

var message = "your message here";    
throw new HttpResponseException(
            Request.CreateErrorResponse(HttpStatusCode.Conflict, message));

https://msdn.microsoft.com/en-us/library/system.net.httpstatuscode(v=vs.110).aspx