Chatra
Chatra

Reputation: 3139

ASP.NET Core Web Api Error for IhttpActionResult

I am trying to create a new Core Web API app for the first time. I am using Core 2.2

I did some research but did not find the correct answer. Not sure if I am using wrong libraries.

cannot implicitly convert type microsoft.aspnetcore.mvc.okresult to system.web.http.Ihttpactionresult

Here is my Code

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);
    }
}

Upvotes: 2

Views: 4994

Answers (2)

juunas
juunas

Reputation: 58898

You need to use IActionResult, not IHttpActionResult.

The IHttpActionResult interface does not exist in Core, it's used in older Web API projects.

Upvotes: 10

bharney
bharney

Reputation: 41

[HttpGet]
public async Task<IActionResult> GetAllUsers()
{
    var users = await this.GetUserAsync()
    return Ok(users);
}

Try changing the type to IActionResult. According to Microsoft docs: the following components don't exist in ASP.NET Core:

IHttpActionResult interface

And I don't believe you need this.Ok, it should be fine using just Ok.

Upvotes: 3

Related Questions