c# - 使用 ApiController 连接 HttpSelfHostServer

标签 c# visual-studio-2013 asp.net-web-api

我有一个派生自 ApiController 的简单类 TestController并处理 POST Http 请求。我还有一个 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。我如何将对 http://localhost:12357 的所有 POST 请求路由到 TestController 类中?

最佳答案

想通了。我只需要在 Main

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

关于c# - 使用 ApiController 连接 HttpSelfHostServer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28255869/

相关文章:

c++ - 使用变成共享库的 matlab 函数时无法解析的符号

c# - 如何从 AuthorizeAttribute 自定义类的 HttpActionContext 获取 cookie?

c# - 如果浏览器关闭,GoogleWebAuthorizationBroker.AuthorizeAsync() 会挂起

c# - 在 C# 中检查一个 VB.NET 对象是否为 null 会产生意外的编译错误

带有 X509Certificate2 的 C# HttpClient - WebException : The request was aborted: Could not create SSL/TLS secure channel

visual-studio-2013 - 你如何在visual studio中添加对dll的引用?

asp.net-web-api - 如何延迟我的 Web API 的响应

c# - 模型绑定(bind)不适用于 asp.net core web api Controller 操作方法中的 Stream 类型参数。(即使使用自定义流输入格式)

c# - 如何在 ActiveDirectory 和 .NET 3.5 中确定用户所属的所有组(包括嵌套组)

c# - 如何创建抽象用户控件并从中派生其他子控件?