2015-01-31 33 views
1

我有一個簡單的類TestController,它源自ApiController並處理POST Http請求。我也有上http://localhost:12357用ApiController連接HttpSelfHostServer

using System; 
using System.Net.Http; 
using System.ServiceModel; 
using System.Web.Http; 
using System.Web.Http.SelfHost; 

namespace RestController 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var config = new HttpSelfHostConfiguration("http://localhost:12357"); 
      using (HttpSelfHostServer server = new HttpSelfHostServer(config)) 
      { 
       server.OpenAsync().Wait(); 
       Console.WriteLine("Press Enter to quit."); 
       Console.ReadLine(); 
      } 
     } 
    } 

    public class TestController : ApiController 
    { 
     [HttpPost] 
     public HttpResponseMessage PostMethodFactory() 
     { 
      return new HttpResponseMessage(); 
     } 
    } 
} 

我試圖找出如何將TestController連接到HttpSelfHostServer監聽的HttpSelfHostServer一個實例。我如何使所有POST請求http://localhost:12357路由到TestController類?

回答

3

想通了。我只需在Main中添加以下行

config.Routes.MapHttpRoute("default", "{*url}", new { controller = "Test" });