aspnet / Mvc

[Archived] ASP.NET Core MVC is a model view controller framework for building dynamic web sites with clean separation of concerns, including the merged MVC, Web API, and Web Pages w/ Razor. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
5.62k stars 2.14k forks source link

Returning Task returns 200 OK even though downstream may have returned NotFoundResult. #8785

Closed VictorioBerra closed 5 years ago

VictorioBerra commented 5 years ago

Is this a Bug or Feature request?:

Bug

Steps to reproduce (preferably a link to a GitHub repo with a repro project):

        // This returns 200 OK even though GetNumber returns NotFound!!!
        [HttpDelete("{id}")]
        public Task DeleteAsyncNotWorking(int id)
        {
            return GetNumber();
        }

        // This works as intended.
        [HttpDelete("working/{id}")]
        public Task<ActionResult<int>> DeleteAsyncWorking(int id)
        {
            return GetNumber();
        }

        private async Task<ActionResult<int>> GetNumber()
        {
            await Task.CompletedTask;

            return NotFound();
        }

Description of the problem:

GetNumber() returns NotFound(). If you call Task DeleteAsyncNotWorking with something like Postman you will see 200OK. This is really confusing... I have to go to all of my methods now and make sure they return the correct codes.

Version of Microsoft.AspNetCore.Mvc or Microsoft.AspNetCore.App or Microsoft.AspNetCore.All:

2.2.0

mkArtakMSFT commented 5 years ago

Thanks for contacting us, @VictorioBerra. The behavior you're seeing here is by-design. If you wish your action to return something relatively generic, you can use the Task<ActionResult> as a return type. Otherwise, if you know the exact type you're going to return, Task<ActionResult<T>> is a great option to stick with.