Why should I return ActionResult instead of object?

Viewed 7106

Assuming that I have application .NET Core 2.1 Web API + Angular 7

Why should I always returns ActionResult? Is there any difference between this:

public ActionResult<MyDTO> GetData(){
    return new MyDTO();
}

and that:

public MyDTO GetData(){
    return new MyDTO();
}

In both cases I'll receive object on UI, and both returns code 200. So is just good practice to using ActionResult, or what?

5 Answers

When you use this:

public MyDTO GetData(){
    return new MyDTO();
}

You cannot return anything that's not an instance of MyDTO, besides throwing an exception.

When you use IActionResult<T> you are saying that you may return an instance of MyDTO, but you don't have to. You can return BadRequest, InternalServerError or whatever suits the API/business needs.

For example:

public IActionResult<MyDTO> GetData() 
{
    if (!User.Identity.IsAuthenticated)
    {
        return Forbidden();
    }

    var data = _someService.GetSomeData();

    if (data == null)
    {
        return NotFound();
    }

    return Ok(data);
}

Basically, when you are returning an object, your code is bound to deliver that object.

In your scenario, all is fine and a MyDTO is returned along with the http 200.

But lets consider this scenario:

public MyDTO GetData(){

    if (someValidationFailed)
    {
        //bad request, not authorized, forbidden etc.
        return BadRequest();
    }

    return new MyDTO();
}

That wouldn't add up. So in general: returning an IActionResult gives you some more flexibility.

What is an ActionResult?

ActionResult is an abstract class that represents the result of an action method. The class itself inherits from System.Object, and only adds one additional abstract method: ExecuteResult, which is an abstract method that the derived classes of ActionResult will implement themselves.

Why is ActionResult an abstract class? So that different controller actions can return different types of results and still have MVC handle them properly.

You may have guessed that this implementation is done because ActionResult has a lot of derived classes, and you'd be exactly right. But what exactly are these different kinds of results? they are categorized into three sections: content-returning, redirection, and status results, and we'll take a look at each kind in turn.

The IActionResult return type is appropriate when multiple ActionResult return types are possible in an action.

The ActionResult types represent various HTTP status codes. Any non-abstract class deriving from ActionResult qualifies as a valid return type.

Some common return types in this category are BadRequestResult (400), NotFoundResult (404), and OkObjectResult (200).

Alternatively, conventional methods in the ControllerBase class can be used to return ActionResult types from an action.

Reference1 Reference2

It depends on what you need your application to do. ActionResult is a default implementation for the IActionResult interface, which has an ExecuteResultAsync(ActionContext) method. So if you would like to execute your result asynchronously with a given context, then you want to use that rather than reinventing the wheel as they say.

If you don't need all that, then return your object and that's fine.

Related