WebApi系列~自主宿主HttpSelfHost的問題

測試中遇到如下問題:

<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");

        }
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章