0

I have an application build by Winform C#. In my application have code to selfhost html file and Web API but the self host web API needs administrator permission. So, I run as administrators on my application, while accessing the html page in my browser. The html page not loaded and I only got a error message in browser.

{"Message":"No HTTP resource was found that matches the request URI 'http://192.168.100.126:44811/'."}

I don't understand why it is not loaded when run as administrator

here code to selfhost html file and web api

void WebHost(string url) {
    try {

        WebApp.Start(url, builder = >builder.UseFileServer(enableDirectoryBrowsing: true));

        var config = new HttpSelfHostConfiguration(url);

        config.Routes.MapHttpRoute(
        name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new {
            id = RouteParameter.Optional
        });
        config.Formatters.Remove(config.Formatters.XmlFormatter);

        var server = new HttpSelfHostServer(config);
        var task = server.OpenAsync();
        task.Wait();
        Console.WriteLine("Server is up and running");

        Console.WriteLine("Listening at " + url);
    } catch(Exception w) {
        MessageBox.Show(w.ToString());
    }
}

and here Image error message when my application run without administrators Image

edit1:

i found another method to selfhost web API. is using webapp.start method. same with selfhost the html file. but when i try to start. i got conflicts prefix

System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'

HttpListenerException: Failed to listen on prefix 'http://192.168.100.126:44811/' because it conflicts with an existing registration on the machine.

i following this tutorial https://learn.microsoft.com/en-us/aspnet/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api

and here code to start

var url = "http://192.168.100.126:44811";
            WebApp.Start(url, builder => builder.UseFileServer(enableDirectoryBrowsing: true));
            WebApp.Start<WebAPI>(url: url);

so the question is how to make it selfhost together in same method (webApp.Start)

edit2:

i try to start selfhost the html file and web API in different port and same method. now is working without conflict.

but can it run in same ip and port without get conflict?.

because i get CORS error when request to web API using Ajax in Jquery

Nyako
  • 11
  • 6

1 Answers1

0

you can avoid running your endpoint as admin every time sth. similar to

 netsh http add urlacl url=http://localhost user=Everyone

Anyway, your error looks like a routing problem. so the request is reaching WebApi but cannot be routed correctly

Falco Alexander
  • 3,092
  • 2
  • 20
  • 39
  • i try to add my ip and port with your command (netsh http add urlacl url=http://192.168.100.126:44811 user=Everyone) on cmd. but is still get error like on image when running without administrators. – Nyako Jul 25 '19 at 09:23
  • yes. the url has been successfully added with the command from you. but I still got an error on selfhost web api when it was run without an administrator – Nyako Jul 26 '19 at 11:06
  • and i found somthing weird. in selfhost web api error message. registering address "http://+:44811". in code i registering address "http ://192.168.100.126:44811" – Nyako Jul 26 '19 at 14:32