简体   繁体   English

无法决定任务 <IActionResult> ,IActionResult和ActionResult <Thing>

[英]Can't decide between Task<IActionResult>, IActionResult and ActionResult<Thing>

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. 虽然我确实了解TaskActionResult等的概念。但是我仍然不确定如果未指定其他任何内容,那么键入控制器将是最直观的。

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: 但是,要使用通用类型的API范例,我应该使用以下代码:

[HttpGet] public IActionResult Get()
{
  return Ok(Context.Things);
}

Finally, respecting the asynchronous nature of the API philosophy I should apply the following: 最后,考虑到API哲学的异步性质,我应该应用以下内容:

[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. 我更喜欢问不确定如何判断哪些是相关的。

Here is a quick comparison of the different return options: 这是不同返回选项的快速比较:

Specific type 具体类型

public Thing Get() {
    return Ok(Context.Things);
}

This is OK if the action will always return one possible type. 如果操作将始终返回一种可能的类型,则可以。 However, most actions may return exceptions (ie status codes other than 200) that have different types. 但是,大多数操作可能会返回具有不同类型的异常(即200以外的状态代码)。

IActionResult type IActionResult类型

This solves the problem above as the IActionResult return type covers different return types. 由于IActionResult返回类型涵盖了不同的返回类型,因此可以解决上述问题。

public IActionResult Get() {
    Thing thing = Context.Things;
    if (thing == null)
        return NotFound();
    else
        return Ok(thing);
}

For asynchronous action, use Task<IActionResult> : 对于异步操作,请使用Task<IActionResult>

public async Task<IActionResult> Get() {
    Thing thing = await Context.Things;
    if (thing == null)
        return NotFound();
    else
        return Ok(thing);
}

ActionResult type ActionResult类型

ASP.NET Core 2.1 introduced the ActionResult<T> return type which offers the following benefits over the IActionResult type: ASP.NET Core 2.1引入了ActionResult<T>返回类型,它比IActionResult类型具有以下优点:

1- The action's expected return type is inferred from the T in ActionResult<T> . 1-从ActionResult<T>T推断动作的预期返回类型。 If you decorate your action with the [ProducesResponseType] attribute, you no longer need to explicitly specify its Type property. 如果使用[ProducesResponseType]属性装饰动作,则不再需要显式指定其Type属性。 For example, you can simply use [ProducesResponseType(200)] instead of [ProducesResponseType(200, Type = typeof(Thing))] . 例如,您可以简单地使用[ProducesResponseType(200)]而不是[ProducesResponseType(200, Type = typeof(Thing))]

2- T converts to ObjectResult , which means return new ObjectResult(T); 2- T转换为ObjectResult ,这意味着return new ObjectResult(T); is simplified to return T; 简化为return T; .

public ActionResult<Thing> Get() {
    Thing thing = Context.Things;
    if (thing == null)
        return NotFound();
    else
        return thing;
}

For asynchronous action, use Task<ActionResult<T>> : 对于异步操作,请使用Task<ActionResult<T>>

public async Task<ActionResult<Thing>> Get() {
    Thing thing = await Context.Things;
    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 . 有关更多详细信息,您可以参考ASP.NET Core Web API中的MSDN页面“ 控制器操作返回类型”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM