3
我正在使用.net web api開發一些寧靜的web服務。.Net Web API針對404/400響應拋出異常/返回響應/返回錯誤響應?
我需要在某些情況下向客戶端返回404(NotFoundStatus)或400(BadRequest)。
看來在框架中有很多方法可以做到這一點。以下列舉了我所瞭解的一些方法。
是否有任何指導選擇哪一個用於返回這些響應?
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Error");
return Request.CreateResponse(HttpStatusCode.NotFound, "Error");
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent("Error") });