【问题标题】:IHttpActionResult method cannot return NotFound()IHttpActionResult 方法无法返回 NotFound()
【发布时间】:2014-08-02 03:34:40
【问题描述】:

我正在为我的网站开发 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);
        }

    }
}

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

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

如果有人能提供可行的解决方案,我将不胜感激! 谢谢。

【问题讨论】:

    标签: c# asp.net-mvc asp.net-web-api


    【解决方案1】:

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

    public class AccountController : ApiController
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-27
      • 2018-05-31
      • 1970-01-01
      • 2014-08-09
      • 1970-01-01
      • 2013-12-07
      • 2019-05-05
      • 1970-01-01
      相关资源
      最近更新 更多