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