From the course: Building Web APIs with ASP.NET Core 8

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Handling errors

Handling errors

- [Instructor] We just got an HTTP 204 status code, I just looked it up in the specification, it means no content. And that absolutely makes sense, we were asking for the item with the ID 42, there is no such item, so there's nothing to server could have returned. But remember that in the REST principle, the HTTP status codes are taken into account, and there is a status code for we can't find anything, that's HTTP 404 not found. So actually we should return that in those cases. And this is how error handling works in general. Here's our implementation so far. So we have our action method, it's finding a product and then it does return okay of product, which means HTTP status code 200 converted or serialized into JSON will be returned. But what if there is no such product? We would like to return HTTP 404 and since the status code that's called okay, 200, is represented by the okay method, you guessed right there is also a not found method and return not found essentially returns HTTP…

Contents