问题
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);
}
}
回答1:
You need to use IActionResult, not IHttpActionResult.
The IHttpActionResult interface does not exist in Core, it's used in older Web API projects.
回答2:
[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.
来源:https://stackoverflow.com/questions/55029185/asp-net-core-web-api-error-for-ihttpactionresult