Hi,
What are the standard ways to return HTTP status codes for validation errors?
I am doing the following pattern in my ASP.NET Web API
| Success | 200 | OK |
| Data Validation Error | 200 | OK |
| Exception | 500 | Internal Server Error |
Hi,
What are the standard ways to return HTTP status codes for validation errors?
I am doing the following pattern in my ASP.NET Web API
| Success | 200 | OK |
| Data Validation Error | 200 | OK |
| Exception | 500 | Internal Server Error |
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sachin SinghRank #66 · 26.2K reputationPosted Jun 21, 2021, 7:23 AM EDT
Sachin SinghRank #66 · 26.2K reputationPosted Jun 21, 2021, 2:49 PM EDT
No, it is perfectly fine to use your own implementation, with Ok(result), as a developer I don't think it is not acceptable or a violation.
Yes, we should try to follow the Rest principles, which say that respect the HTTP protocol means sending the appropriate status code with the response.
There are many HTTP status codes like 303 that the result of a post request matches with any existing representation.
422 unprocessable entity etc.
but 200 is also a status code at the end, so is fine as long as the response clearly describes the problem.
Sachin SinghRank #66 · 26.2K reputationPosted Jun 21, 2021, 5:25 AM EDT
if you are using IHttpActionResult instead of HttpResponseMessage then we already have helper methods for necessary status codes as
public IHttpActionResult Get()
{
var empList= db.Employees.ToList();
return Ok(empList);
}
public IHttpActionResult Get(int id)
{
var employee = FetchFromDB().FirstOrDefault(e => e.Id == id);
if (employee == null)
{
//return NotFound();
return Content(HttpStatusCode.NotFound, "employee not found");
}
return Ok(employee);
}
Praveen KumarRank #395 · 6.4K reputationPosted Jun 21, 2021, 7:16 AM EDT
Praveen KumarRank #395 · 6.4K reputationPosted Jun 21, 2021, 11:54 AM EDT