1

I try to setup a HttpSelfHostServer in C#. I use this class to start the server:

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();
    }
}

Moreover, I have a Controller-class which looks like:

class AutomaticTestController : ApiController
{

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

When I open http://localhost:8080/api/AutomaticTest/status in a web browser, I get the following result:

<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>

Obviously, I made something wrong, but I don't know what. Does anybody have a hint? I wonder how the HttpSelfHostServer instance finds the AutomaticTestControllerClass. I assume that I have create some link to the class? If so, how do I do that?

Thanks a lot for all constructive answers.

SomeBody
  • 7,515
  • 2
  • 17
  • 33
  • I'm not sure, But as a try `routeTemplate: "api/{controller}/{action}/{test}"` instead, and without `controller = "AutomaticTest",`, and I also prefer `name: "Default"` -HTH ;). – shA.t Oct 11 '17 at 16:47
  • 1
    If AutomaticTestController has the modifier public, the program works. But still I wonder how the instance of HttpSelfHostServer knows about the AutomaticTestController class. – SomeBody Oct 16 '17 at 15:14
  • I think [this answer](https://stackoverflow.com/a/397451/4519059) can help you ;). – shA.t Oct 17 '17 at 08:41

0 Answers0