How to return HTTP Status Codes from API?

I want to return proper status codes if something went wrong, e.g. 404 if a resource is not found or 403 if the permission is not sufficient.

In .net web api, one can express that with the return type. If you use IHttpActionResult or HttpResponseMessage you can modify the headers beside the content.
So I wonder if there's a similar method to return the status code beside the content in the API designer?

I tried it with HttpResponseMessage but had no luck (it returns the object as JSON with status code 200).

Parents Reply
  • Hi Wolfgang,

    You could emit any status code without using a HttpException, but it's a bit more complex.

    It's true that the default model focuses on the data, and abstracts away stuff like HTTP headers, status codes and content types. There will be a more flexible way to configure the underlying HttpResponseMessage object starting with version 8.2.

    Do you have a specific use case for the status codes 1xx-3xx in mind?

    Hanno

Children