locked
how to return NotFound() result RRS feed

  • Question

  • I am trying to return NotFound() result. 

     [Route("~/api/patient/{patientId:int}/reactions/{id:int?}")]
            [ResponseType(typeof(IEnumerable<Reaction>))]
            public async Task<IHttpActionResult> GetReactionsByPatientId(int patientId, int id = 0)
            {
                return await Task.Run(() =>
                {
                    IEnumerable<Reaction> reactionsByPatient = ReactionRepo.GetReactionsByPatientId(patientId).Result;
    
                    if (reactionsByPatient == null)
                    {
                        return NotFound();
                    }
    
                    if (id != 0)
                    {
                        reactionsByPatient = reactionsByPatient.Where(I => I.Id == id);
                    }
    
                    if (reactionsByPatient == null)
                    {
                        return NotFound();
                    }
    
                    return Ok(reactionsByPatient);
                });
            }

    the error messages are

    CS0029 C# Cannot implicitly convert type to 'System.Threading.Tasks.Task'

    CS8030 C# Anonymous function converted to a void returning delegate cannot return a value

    CS1662 C# Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type

    Monday, June 11, 2018 10:06 AM

All replies

  • Hello,

    See the following which has examples to set and return. Like Ok, NoFound will return a 404 status code.

    https://weblogs.asp.net/dwahlin/new-features-in-asp-net-web-api-2-part-i


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, June 11, 2018 10:33 PM