ASP.NET 5 Kestrel 在局域网内连接

ASP.NET 5 Kestrel connect within LAN

我想从同一网络中的另一台 PC 连接到我的 Kestrel 服务器,其中托管了 ASP.NET 5 个应用程序。可能吗?我可以从 cmd ping 我的计算机,但是当我尝试从网络浏览器连接时我得到 'Connection timed out'(我输入:"http://{my_kestrel_ip}:5000/")。

在您的项目文件夹中,您应该有一个名为 hosting.ini 的文件。默认情况下,在那个文件中你应该有这样的东西:

server=Kestrel
server.urls=http://localhost:5000

您需要让 HTTP 服务器侦听您的 public IP 地址以及本地主机。为此,您可以通过用分号分隔它们来添加一个额外的地址:

server.urls=http://localhost:5000;http://{my_kestrel_ip}:5000

hosting.ini 不适合我们。我必须将其添加到 project.json 文件中。我相信 hosting.ini 文件在 Beta8 之后将被弃用。

--server.urls http://0.0.0.0:5000

或者我更喜欢以下我认为不那么令人困惑的内容。

--server.urls http://*:5000

所以你最终会在你的 project.json 中得到这样的东西。

"commands": {
        "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.Kestrel --server.urls http://0.0.0.0:5000",
        "ef": "EntityFramework.Commands"
    },

刚刚做了一个似乎有效的快速测试。 在 project.json 文件旁边创建一个 hosting.json 文件。

hosting.json:

{
    "server.urls": "http://localhost:5000;http://192.168.1.4:5000"
}

project.json:

"commands": {
    "web": "Microsoft.AspNet.Server.Kestrel --config hosting.json"
},

在命令提示符中只需 运行 dnx web,输出:

Hosting environment: Production
Now listening on: http://localhost:5000
Now listening on: http://192.168.1.4:5000
Application started. Press Ctrl+C to shut down.

你会得到一个防火墙提示,接受它,然后 tadaaa!!您可以从局域网和本地主机访问该站点。