Reputation: 6083
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?
Upvotes: 14
Views: 17103
Reputation: 32068
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);
}
Upvotes: 23
Reputation: 1577
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.
Upvotes: 8
Reputation: 94
Using ActionResult instead of an object, you are able to return different response types for different situations (errors, redirects ..)
Returning an object instead of an ActionResult returns always a 200 response (supossing no Exception was thrown)
For a complete list of the results and the methods used to return them, please, check:
Upvotes: 0
Reputation: 17658
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.
Upvotes: 4
Reputation: 35290
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.
Upvotes: 0