我正在使用web api构建我的第一个应用程序,而且我偶然发现了一个无法找到信息的问题。找不到与控制器匹配的类型
使用控制器的样品来测试其工作:
public class TestController : ApiController
{
// GET api/test
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
WebApiConfig.cs:
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
每次我打电话使用http://localhost:59502/api/test
服务我得到
<Error>
<Message>
No HTTP resource was found that matches the request URI 'http://localhost:59502/api/test'.
</Message>
<MessageDetail>
No type was found that matches the controller named 'test'.
</MessageDetail>
</Error>
有人能指出我做错了什么吗?
如果您删除'config.MapHttpAttributeRoutes()'它是否工作?我可能是错的,但如果您使用该选项,我认为您还需要使用路线上的属性,例如'[Route(“api/test/{id}”)]' – James 2014-11-06 16:50:28
刚刚尝试 - 仍然没有去。 :( – Savio 2014-11-06 16:52:12
@Savio,为什么要''测试''路由到''Get'“? – 2014-11-06 16:53:45