測試中遇到如下問題:
<Error>
<Message>
No HTTP resource was found that matches the request URI 'http://localhost:37331/api/action/FindByModule?moduleId=1'.
</Message>
<MessageDetail>
No type was found that matches the controller named 'action'.
</MessageDetail>
</Error>
解決辦法:
controler類的名字要和 訪問的action名字一致,也就是說標紅的名字要一致。
http://localhost:37331/api/action/FindByModule?moduleId=1
public class ActionController : ApiController
{
[HttpGet]
// [ActionName("FindByModule")]
public string FindByModule(Int64 moduleId)
{
return "ddddd";
}
}
class WebApiService
{
public WebApiService()
{
var config = new HttpSelfHostConfiguration("http://localhost:37331");
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional });
var server = new HttpSelfHostServer(config);
server.OpenAsync().Wait();
Console.WriteLine("Web Api Server is opened");
}
}