Web API self host - bind on all network interfaces

2019-03-17 10:26发布

问题:

How do you make a Web API self host bind on all network interfaces?

I have the below code currently. Unfortunately, it binds only on localhost. So access to this server from other than localhost is failing.

var baseAddress = string.Format("http://localhost:9000/"); 
            using (WebApp.Start<Startup> (baseAddress)) 
            {
                Console.WriteLine("Server started");
                Thread.Sleep(1000000);
            }

回答1:

Just change the base address like this

        var baseAddress = string.Format("http://*:9000/"); 
        using (WebApp.Start<Startup> (baseAddress)) 
        {
            Console.WriteLine("Server started");
            Thread.Sleep(1000000);
        }

And it should bind correctlly to all interfaces.



回答2:

bind it using :-

var baseAddress = string.Format("http://localhost:+:9000/"); 

If you get access exceptions, Please DO NOT start Visual Studio as admin user. Add an URL reservation instead. Start a command console window as administrator and execute:

netsh
netsh> http add urlacl url="http://+:9000/" sddl=D:(A;;GX;;;S-1-1-0)

The SDDL translates to "all users" from your current domain / machine.