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

前端 未结 10 2093
情书的邮戳
情书的邮戳 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:50

    You could use ResponseMessageResult if you like:

    var myCustomMessage = "your custom message which would be sent as a content-negotiated response"; 
    return ResponseMessage(
        Request.CreateResponse(
            HttpStatusCode.NotFound, 
            myCustomMessage
        )
    );
    

    yeah, if you need much shorter versions, then I guess you need to implement your custom action result.

提交回复
热议问题