How do I return NotFound() IHttpActionResult with an error message or exception?

前端 未结 10 2091
情书的邮戳
情书的邮戳 2020-12-02 07:18

I am returning a NotFound IHttpActionResult, when something is not found in my WebApi GET action. Along with this response, I want to send a custom message and/

10条回答
  •  粉色の甜心
    2020-12-02 07:54

    You may use ReasonPhrase property of HttpResponseMessage class

    catch (Exception exception)
    {
      throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)
      {
        ReasonPhrase = exception.Message
      });
    }
    

提交回复
热议问题