使用 ApiController 连接 HttpSelfHostServer

Hooking up HttpSelfHostServer with an ApiController

我有一个简单的 class TestController 派生自 ApiController and handles POST Http requests. I also have an instance of 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();
        }
    }
}

我正在尝试弄清楚如何将 TestController 连接到 HttpSelfHostServer。如何将所有 POST 请求发送到 http://localhost:12357 路由到 TestController class?

想通了。我只需要在 Main

中添加以下行
config.Routes.MapHttpRoute("default", "{*url}", new { controller = "Test" });