IhttpActionResult的ASP.NET Core Web Api错误

问题描述 投票:0回答:2

我想第一次创建一个新的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);
    }
}
asp.net-core asp.net-core-webapi
2个回答
2
投票

你需要使用IActionResult,而不是IHttpActionResult

核心中不存在IHttpActionResult接口,它用于较旧的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就可以了。

© www.soinside.com 2019 - 2024. All rights reserved.