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

前端 未结 10 2103
情书的邮戳
情书的邮戳 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 08:17

    Iknow PO asked with a message text, but another option to just return a 404 is making the method return a IHttpActionResult and use the StatusCode function

        public async Task Get([FromUri]string id)
        {
           var item = await _service.GetItem(id);
           if(item == null)
           {
               StatusCode(HttpStatusCode.NotFound);
           }
           return Ok(item);
        }
    

提交回复
热议问题