所有的ASP.NET Web API控制器返回404 [英] All ASP.NET Web API controllers return 404
问题描述
我试图得到一个API控制器到ASP.NET MVC 4 Web应用程序内工作。但是,每个请求的结果在 404 ,我很为难。 :/
I'm trying to get an API Controller to work inside an ASP.NET MVC 4 web app. However, every request results in a 404 and I'm stumped. :/
我从项目模板类似定义的标准API控制器路线:
I have the standard API controller route from the project template defined like:
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中调用:
The registration is invoked in Global.asax:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
// Register API routes
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
我有一个基本的API控制器是这样的:
I have a basic API controller like this:
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)
{
}
}
}
现在,当我浏览到的本地主机:59900 / API /收藏我想到获取以被调用的方法,而是我得到的 404 状态code和以下响应:
Now, when I browse to localhost:59900/api/Favorites I expect the Get method to be invoked, but instead I get a 404 status code and the following response:
<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>
任何帮助将大大AP preciated,我失去了我的心一点点在这里。 :)谢谢!
Any help would be greatly appreciated, I'm losing my mind a little bit over here. :) Thanks!
推荐答案
有一件事情我碰到了已经被注册我的配置以错误的顺序在我的Global.asax文件,例如:
One thing I ran into was having my configurations registered in the wrong order in my GLobal.asax file for instance:
权秩序:
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
错误顺序:
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
WebApiConfig.Register(GlobalConfiguration.Configuration);
只是说,这是我的问题,改变的顺序是明显的,但有时被忽略,并可能导致太多的无奈。
Just saying, this was my problem and changing the order is obvious, but sometimes overlooked and can cause much frustration.
这篇关于所有的ASP.NET Web API控制器返回404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!