【问题标题】:ASP.NET Core Web Api Error for IhttpActionResultIhttpActionResult 的 ASP.NET Core Web Api 错误
【发布时间】:2021-09-03 03:47:44
【问题描述】:

我第一次尝试创建一个新的 Core Web API 应用程序。我正在使用 Core 2.2

我做了一些研究,但没有找到正确的答案。不确定我是否使用了错误的库。

无法将类型 microsoft.aspnetcore.mvc.okresult 隐式转换为 system.web.http.Ihttpactionresult

这是我的代码

using System.Threading.Tasks;
using System.Web.Http;
using Microsoft.AspNetCore.Mvc;

[Microsoft.AspNetCore.Mvc.Route("api/User")]
[ApiController]
public class UserController : ControllerBase
{
    [Microsoft.AspNetCore.Mvc.HttpGet]
    public async Task<IHttpActionResult> GetAllUsers()
    {
        var users = await this.GetUserAsync();
        return this.Ok(users);
    }
}

【问题讨论】:

  • 除了下面的答案,删除System.Web 依赖。这会导致各种各样的问题。 ASP.NET Core 不使用 System.Web,因此如果您看到来自该命名空间的内容,则说明您做错了。

标签: asp.net-core asp.net-core-webapi


【解决方案1】:

您需要使用IActionResult,而不是IHttpActionResult

IHttpActionResult 接口在 Core 中不存在,它在较旧的 Web API 项目中使用。

【讨论】:

    【解决方案2】:
    [HttpGet]
    public async Task<IActionResult> GetAllUsers()
    {
        var users = await this.GetUserAsync()
        return Ok(users);
    }
    

    尝试将类型更改为 IActionResult。 According to Microsoft docs: ASP.NET Core 中不存在以下组件:

    IHttpActionResult接口

    我不相信你需要这个。好的,使用 Ok 应该没问题。

    【讨论】:

      猜你喜欢
      • 2018-12-17
      • 1970-01-01
      • 2014-04-23
      • 2019-04-10
      • 2017-02-03
      • 2019-06-25
      • 2019-11-04
      • 2020-10-05
      • 2020-12-23
      相关资源
      最近更新 更多