使用ApiController连接HttpSelfHostServer

tcb*_*tcb 1 c# asp.net-web-api visual-studio-2013

我有一个简单的类TestController,它派生自ApiController并处理POSTHttp请求。我也有一个监听的HttpSelfHostServer实例http://localhost:12357

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我试图弄清楚如何连接TestControllerHttpSelfHostServer。如何提出所有路由到班级的POST请求?http://localhost:12357TestController

tcb*_*tcb 5

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

config.Routes.MapHttpRoute("default", "{*url}", new { controller = "Test" });
Run Code Online (Sandbox Code Playgroud)