IHttpActionResult方法无法返回NotFound()

rod*_*dit 12 c# asp.net-mvc asp.net-web-api

我正在为我的网站开发一个Web API,并遇到了一个问题.

目前,API应该返回指定用户的详细信息.

这是我的帐户控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Http;
using System.Net;
using RoditRepoAPIV2.Models;

namespace RoditRepoAPIV2.Controllers
{
    public class AccountController : Controller
    {
        Account[] products = new Account[] 
        { 
            //accounts will be added...
        };

        public IEnumerable<Account> GetAllAccounts()
        {
            return products;
        }

        public IHttpActionResult GetAccount(int id)
        {
            var account = products.FirstOrDefault((p) => p.Id == id);
            if (account == null)
            {
                return NotFound();
            }
            return Ok(account);
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

尽管此处的大部分代码都是从教程中复制的,但Visual Studio却抱怨当前上下文中不存在NotFound()"和" Ok(account).我已将所有 NuGet软件包更新到版本5.1.2+,但仍然出现此错误.

我做了一些研究,发现它似乎对其他人有用......

如果有人能用一个有效的解决方案作出回应,我将不胜感激!谢谢.

And*_*rei 28

您需要继承您的控制器ApiConroller- 这是定义这些方法的地方:

public class AccountController : ApiController
Run Code Online (Sandbox Code Playgroud)