简体   繁体   English

ASP.NET 核心任务<iactionresult>没有异步就无法返回 NotFound() 和 OK()</iactionresult>

[英]ASP.NET Core Task<IActionResult> cannot return NotFound() and OK() without async

This function gives an error:这个 function 给出了一个错误:

[HttpPost]
public Task<IActionResult> Something (string value)
{
     Student student = new Student();
     if (value == "LOW")
     {
         return NotFound();
     }

     return Ok();
}

This function works fine:这个 function 工作正常:

[HttpPost]
public async Task<IActionResult> Something (string value)
{
     Student student = new Student();
     if (value == "LOW")
     {
         return NotFound();
     }
     return Ok();
}

When I am trying to return NotFound();当我试图return NotFound(); or return Ok();return Ok(); when I am not including keyword asnyc before Task as shown above, it gives me error.如上所示,当我在Task之前不包括关键字asnyc时,它会给我错误。 However, when i added the async keyword in front of Task , it does not shows any error in the compiler.但是,当我在Task前面添加async关键字时,它不会在编译器中显示任何错误。 Can you tell me what is causing this?你能告诉我这是什么原因造成的吗?

The async keyword tells the compiler that while the method returns a Task , the code within the method is expected to run in an asynchronous manner and internally await something. async关键字告诉编译器,当方法返回一个Task时,方法中的代码应该以异步方式运行并在内部await某些东西。 The compiler will look for one or more await s and modify the structure of the method to wrap functionality around tasks and continuations of tasks.编译器将查找一个或多个await并修改方法的结构以将功能包装在任务和任务的延续中。 So ultimately the compiler will wrap the returned result in a Task<T> for you.所以最终编译器会为你将返回的结果包装在一个Task<T>中。

Basically, your logic can focus on the value you want to return, rather than focus on the structure of asynchronicity.基本上,你的逻辑可以专注于你想要返回的值,而不是专注于异步的结构。 The async and await keywords exist to make that part simpler.存在asyncawait关键字以使该部分更简单。

So both of these methods actually have a problem:所以这两种方法其实都有一个问题:

  1. The first method is not async , so the compiler isn't expecting to modify it in any way.第一种方法不是async ,因此编译器不希望以任何方式修改它。 Instead, the compiler is expecting it to return what it says it will return, which is a Task<IActionResult> .相反,编译器期望它返回它说它将返回的内容,即Task<IActionResult> It doesn't return that, so the compiler produces an error.它不会返回,因此编译器会产生错误。
  2. The second method is async , but nowhere in that method do you await anything.第二种方法是async ,但在该方法中您无处await任何东西。 The compiler can make this work, but produces a warning that nothing is being awaited in this async method.编译器可以完成这项工作,但会产生警告,表明此async方法中没有等待任何内容。

So both are wrong.所以两者都是错误的。 Since your method doesn't do anything asynchronous, don't make it async :由于您的方法不执行任何异步操作,因此不要将其async

[HttpPost]
public IActionResult Something(string value)
{
    Student student = new Student();
    if (value == "LOW")
    {
        return NotFound();
    }
    return Ok();
}

You only need to make the method async (and wrap a Task<T> around the return type) if the method internally needs to await something.如果方法在内部需要await某些东西,您只需要使方法async (并在返回类型周围包装一个Task<T> )。

If you do not want async...then your (first) code should be:如果您不想要异步...那么您的(第一个)代码应该是:

 public   IActionResult Something (string value)

(no Task) (无任务)

暂无
暂无

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

相关问题 如何在ASP.NET Core中返回自定义HTTP状态/消息而不返回对象,IActionResult等? - How to return a custom HTTP status/message in ASP.NET Core without returning object, IActionResult, etc? 从ASP.NET Core中的WebSocket请求返回什么IActionResult? - What IActionResult to return from a WebSocket request in ASP.NET Core? 如何在 ASP.NET Core 中返回 403 Forbidden 响应作为 IActionResult - How to return 403 Forbidden response as IActionResult in ASP.NET Core 如何测试我的 ASP.NET Core 2.2 Web API GET IActionResult 返回 Ok(object)? - How to test my ASP.NET Core 2.2 Web API GET IActionResult that returns Ok(object)? 返回“JsonResult / IActionResult”或“Task”有什么不同 <SomeObject> “或者只是Asp.net Core Web API中的”SomeObject“? - What is the difference in returning “JsonResult/IActionResult” or “Task<SomeObject>” or just “SomeObject” in a Asp.net Core Web API? 如何将此IHttpActionResult转换为ASP.NET Core IActionResult - How to translate this IHttpActionResult to ASP.NET Core IActionResult C# 接口概念说明与 ASP.NET Core 中的 IActionResult - C# interface concept clarification with IActionResult in ASP.NET Core 数据表 asp.net 核心剃刀页面 IActionResult 新 JsonResult - datatables asp.net core razor pages IActionResult new JsonResult Asp.net core IActionResult csv 编码问题 - ASp.net core IActionResult csv encoding problem 通过 Asp.NET Core 6 中的 Ok() 获取确切的字符串返回 - Get the exact string return by the Ok() in Asp.NET Core 6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM