【发布时间】: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