How to return a message along with an HttpStatusCode

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 a 404 , otherwise return a OK with the product. My doubts are as follows:

  • How can I return a message along with the status 404 ?
  • Are you having trouble returning a message along with status ?
asked by anonymous 16.06.2016 / 16:04

1 answer

3

How can I return a message along with status 404?

In a simple way, just do this:

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

Are you having trouble returning a message along with the status?

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

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

    
16.06.2016 / 16:07