Web API自托管主机 – 在所有网络接口上绑定

如何在所有网络接口上进行Web API自主绑定?

我目前有以下代码。 不幸的是,它仅绑定在localhost上。 因此,从localhost以外的地方访问此服务器失败。

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

只需像这样更改基址

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

它应该正确绑定到所有接口。

用它绑定它: –

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

如果您获得访问exception,请不要以管理员用户身份启动Visual Studio。 请改为添加url。 以管理员身份启动命令控制台窗口并执行:

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

SDDL转换为当前域/机器的“所有用户”。