我正在使用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