While I do understand the concept of Task
, ActionResult
, etc. I still feel unsure which would be most intuitive to type in a controller if nothing else is specified.
Taking consideration to be as explicit with a type returned, I should go like this:
[HttpGet] public ActionResult<Thing> Get() { return Ok(Context.Things); }
However, going for a generic type of API paradigm I should use this:
[HttpGet] public IActionResult Get() { return Ok(Context.Things); }
Finally, respecting the asynchronous nature of the API philosophy I should apply the following:
[HttpGet] public Task<IActionResult> Get() { return Ok(Context.Things); }
I can't decide between which is most appropriate in a general, green-field scenario. The first two work seemingly. Intuitively, I'd prefer to go with the third one but since it didn't work (the conversion isn't valid), I got worried that perhaps I'm barking up the wrong binary tree.
Not sure at all how to google it and I'm obtaining all kinds of examples. Uncertain how to judge which ones are of relevance, I prefer to ask.
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.
IActionResultis vs ActionResult. IActionResult is an interface and ActionResult is an implementation of that interface. ActionResults is an abstract class and action results like ViewResult, PartialViewResult, JsonResult, etc., derive from ActionResult.
IHttpActionResult is for ASP.NET Web Api, while IActionResult is for ASP.NET Core. There's no such thing as "Web Api" in ASP.NET Core. It's all just "Core". However, some people still refer to creating an ASP.NET Core API as a "Web Api", which adds to the confusion.
ActionResult is an abstract class, and it's base class for ViewResult class. In MVC framework, it uses ActionResult class to reference the object your action method returns. And invokes ExecuteResult method on it. And ViewResult is an implementation for this abstract class.
Here is a quick comparison of the different return options:
public Thing Get() { return Context.Things.GetThing(1234); }
This is OK if the action will always return one possible type. However, most actions may return exceptions (i.e. status codes other than 200) that have different types.
This solves the problem above as the IActionResult
return type covers different return types.
public IActionResult Get() { Thing thing = Context.Things.GetThing(1234); if (thing == null) return NotFound(); else return Ok(thing); }
For asynchronous action, use Task<IActionResult>
:
public async Task<IActionResult> Get() { Thing thing = await Context.Things.GetThing(1234); if (thing == null) return NotFound(); else return Ok(thing); }
ASP.NET Core 2.1 introduced the ActionResult<T>
return type which offers the following benefits over the IActionResult
type:
1- The action's expected return type is inferred from the T
in ActionResult<T>
. If you decorate your action with the [ProducesResponseType]
attribute, you no longer need to explicitly specify its Type
property. For example, you can simply use [ProducesResponseType(200)]
instead of [ProducesResponseType(200, Type = typeof(Thing))]
.
2- T
converts to ObjectResult
, which means return new ObjectResult(T);
is simplified to return T;
.
public ActionResult<Thing> Get() { Thing thing = Context.Things.GetThing(1234); if (thing == null) return NotFound(); else return thing; }
For asynchronous action, use Task<ActionResult<T>>
:
public async Task<ActionResult<Thing>> Get() { Thing thing = await Context.Things.GetThing(1234); if (thing == null) return NotFound(); else return thing; }
For more details, you can refer to the MSDN page Controller action return types in ASP.NET Core Web API.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With