我有一个web API项目,它运行得很好。我将它与一个MVC项目合并,现在只有带有URI参数的操作才能工作。所有其他操作都以404 Not found结束,其中甚至找不到控制器。
下面是我在WebApiConfig中拥有的(标准的东西):
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}下面是控制器类:
[Authorize]
[RoutePrefix("api/WikiPlan")]
public class WikiPlanController : ApiController以下是有效的操作:
http://localhost:2000/api/WikiPlan/SearchWikiPlans/baby
[AllowAnonymous]
[HttpGet]
[Route("SearchWikiPlans/{keyword}")]
[ResponseType(typeof(List<WikiPlanSearchResultViewModel>))]
public IHttpActionResult SearchWikiPlans(string keyword)这里有一个不起作用的(当它在自己的项目中时,它曾经起作用):
http://localhost:2000/api/WikiPlan/TopWikiPlans
[AllowAnonymous]
[HttpGet]
[Route("TopWikiPlans")]
[ResponseType(typeof(List<TopWikiPlan>))]
public IHttpActionResult TopWikiPlans()我不知道哪里出了问题。谢谢你的帮忙!
发布于 2014-07-06 10:57:51
多亏了这个路由调试器工具(http://blogs.msdn.com/b/webdev/archive/2013/04/04/debugging-asp-net-web-api-with-route-debugger.aspx),我能够跟踪损坏的网址并解决问题。
事实证明,框架是根据MVC路由而不是我的API路由匹配损坏的URL。因此,我移动了调用以在Global.asax中的MVC路由之上注册API路由,现在可以正确匹配了。
https://stackoverflow.com/questions/24591888
复制相似问题