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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sachin SinghPosted Jun 21, 2021, 11:23 AM
Sachin SinghPosted Jun 21, 2021, 6:49 PM
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.
Praveen KumarPosted Jun 21, 2021, 3:54 PM
Praveen KumarPosted Jun 21, 2021, 11:16 AM
Sachin SinghPosted Jun 21, 2021, 9:25 AM
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);
}