设置HttpSelfHostServer - 将回复HTTP请求并显示错误消息

时间:2017-10-11 16:32:15

标签: c# web-hosting

我尝试在C#中设置HttpSelfHostServer。我用这个类来启动服务器:

class Route
{
    HttpSelfHostServer server;

    public int Port { get { return 8080;  } }

    public void Routestart()
    {
        string baseUrl = "http://localhost:" + this.Port + "/";
        HttpSelfHostConfiguration config = new HttpSelfHostConfiguration(baseUrl);
        config.Routes.MapHttpRoute(
            name: "test1",
            routeTemplate: "api/AutomaticTest/{Action}/{test}",
            defaults: new
            {
                controller = "AutomaticTest",
                test = RouteParameter.Optional
            });
        server = new HttpSelfHostServer(config);
        server.OpenAsync();
    }
    public void Routestop()
    {
        server.CloseAsync();
    }
}

此外,我有一个Controller类,看起来像:

class AutomaticTestController : ApiController
{

    [HttpGet]
    [ActionName("status")]
    public string Status()
    {
        return "example";
    }
}

当我在网络浏览器中打开http://localhost:8080/api/AutomaticTest/status时,我得到以下结果:

<Error>
<Message>No HTTP resource was found that matches the request URI
'http://localhost:8080/api/AutomaticTest/status'.</Message>
<MessageDetail>No type was found that matches the controller named
'AutomaticTest'.</MessageDetail></Error>

显然,我做错了什么,但我不知道是什么。有人有提示吗?我想知道HttpSelfHostServer实例如何找到AutomaticTestControllerClass。我假设我已经创建了一些类的链接?如果是这样,我该怎么做?

非常感谢所有建设性的答案。

0 个答案:

没有答案