Web Api 2: NotFound () with content?

I have the following resource used to return specific company information:

[HttpGet]
[Route("{companyId:Guid}")]
public IHttpActionResult Get(Guid companyId)
{
    var company = CompanyRepository.Find(companyId);

    if (company == null)
    {
        return NotFound();
    }
    else
    {
        var responseModel = new CompanyResponseModel()
        {
            CompanyId = company.Id,
            Name = company.Name
        };

        return Ok(responseModel);
    }
}

      

Why can't we include content in the challenge NotFound()

?

For example, I would like to include an error response model with the message "Company ID does not exist".

Perhaps it has something to do with RESTful design?

+3


source to share


1 answer


According to RFC2616 Section 10 , 404 does not return any information about the resource itself. But if you want to use 404, you can use this instead:



return Content(HttpStatusCode.NotFound, "Your Content/Message");

      

+8


source







All Articles