找不到WebApi控制器 [英] WebApi controller isn't found
本文介绍了找不到WebApi控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的WebApi项目中有一个示例控制器:
I have an example controller in my WebApi-project:
public class MilestonesController : ApiController
{
// GET api/milestones
public IEnumerable<string> Get()
{
return new string[] { "M#1", "M#2" };
}
// GET api/milestones/5
public string Get(int id)
{
return "M with {id}";
}
// POST api/milestones
public void Post([FromBody]string value)
{
}
// PUT api/milestones/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/milestones/5
public void Delete(int id)
{
}
}
如果我尝试导航到它:
http://localhost:59493/api/milestones
我总是会收到错误:
No HTTP resource was found that matches the request URI 'http://localhost:59493/api/milestones'.
No type was found that matches the controller named 'milestones'.
提前谢谢!
我的WebApiConfig:
my WebApiConfig:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Global.asax:
Global.asax:
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
WebApiConfig.Register(GlobalConfiguration.Configuration);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
可能的解决方案:
我已经删除了我的项目,并且创建了一个新项目(WebAPi项目),现在它可以工作了.WebApiConfig等是相同的,所以我真的不知道第一次尝试出了什么问题
I have deleted my project and I've created a new one (WebAPi project) and now it works. The WebApiConfig etc was the same, so I don't really know what was wrong with my first try
推荐答案
看看您在哪里创建了该控制器.也许它不在Controllers文件夹中,不在其他名称空间中,或者该控制器类嵌套在另一个类中
Take a look at where you created that controller. Maybe it is not in the Controllers folder, different namespace or that controller class is nested into another class
这篇关于找不到WebApi控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文