How to return a message together with an Httpstatuscode

Asked

Viewed 216 times

3

I have the following method in my Controller:

public IHttpActionResult Get(string id) 
    {
        var product = objds.GetProduct(new ObjectId(id));
        if (product == null)
        {
            return NotFound();
        }
        return Ok(product);
    }

If the product is null then return one 404, otherwise return a OK with the product. My doubts are as follows::

  • How can I return a message along with the status 404?
  • Do you have any problem returning a message along with the status?

1 answer

3


How can I return a message along with 404 status?

In a simple way, just do this:

return Content(HttpStatusCode.NotFound, "Foo does not exist.");

Have a problem returning a message along with the status?

There is no problem with this. What usually happens is that you get the answer when making a request to an API. Now, if you want to send a message together, you can do.

There are other ways to implement this same functionality, as shown in this answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.