我试图让一个 API 控制器在 ASP.NET MVC 4网络应用程序中工作。然而,每个请求的结果都是 404,我被难住了。:/
我从项目模板中定义了标准的 API 控制器路由,定义如下:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
在 Global.asax 中调用注册:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
// Register API routes
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
我有一个这样的基本 API 控制器:
namespace Website.Controllers
{
public class FavoritesController : ApiController
{
// GET api/<controller>
public IEnumerable<string> Get()
{
return new [] { "first", "second" };
}
// PUT api/<controller>/5
public void Put(int id)
{
}
// DELETE api/<controller>/5
public void Delete(int id)
{
}
}
}
现在,当我浏览到 Localhost: 59900/api/Favorites时,我希望调用 走开方法,但是我得到的却是 404状态代码和以下响应:
<Error>
<Message>
No HTTP resource was found that matches the request URI 'http://localhost:59900/api/Favorites'.
</Message>
<MessageDetail>
No type was found that matches the controller named 'Favorites'.
</MessageDetail>
</Error>
如果你能帮忙,我将不胜感激,我有点失去理智了。 :)谢谢!