asp.net-core - 如何从公共(public)互联网访问 HTTP 端口 5001

标签 asp.net-core .net-core ip nat windows-server

我有 Windows Server 2016 Data center x64、.NET Core SDK 5.0 预览版、Microsoft SQL Server 2019

enter image description here

在服务器上:https://localhost:5001/publisher/all ok

在服务器上https://127.0.0.1:5001/publisher/all好的

在服务器上:防火墙开放端口 5000-6000 出站

来 self 的电脑(或世界上任何一台电脑)https://45.118.145.72:5011/publisher/all不行

enter image description here

如何访问https://45.118.145.72:5011/publisher/all来自公共(public)互联网?

enter image description here

最佳答案

Asp.NET Core 应用程序使用默认设置绑定(bind)到“localhost”网络接口(interface)。其他主机无法使用此网络接口(interface)。

您可以在主机设置期间使用 UseUrls() 修改此设置。

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
            webBuilder.UseUrls("http://0.0.0.0:5001");
        });

示例:

webBuilder.UseUrls("http://127.0.0.1:5001"); // only from localhost
webBuilder.UseUrls("http://localhost:5001"); // only from localhost
webBuilder.UseUrls("http://0.0.0.0:5001");   // allow all hosts

文档:https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-3.1#kestrel-in-aspnet-core-apps

关于asp.net-core - 如何从公共(public)互联网访问 HTTP 端口 5001,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63010812/

相关文章:

Travis CI 中的 Docker 构建失败 - 模式中出现 "Error checking context: ' 语法错误”

asp.net-core - 在 vscode 调试控制台中禁用符号加载的详细日志记录

c# - 方法 'UseRouting' 没有重载需要 1 个参数

c# - 如何使用 .NET Core 依赖注入(inject)在运行时解析服务和注入(inject)额外的构造函数参数?

c# - 网络核心 : how to separate controllers by ports?

c# - 使用 Entity Framework Core 进行全文搜索

ubuntu - 为什么我的 .NET Core 服务器应用程序在 Ubuntu 中不接受连接,但在 Windows 中可以正常工作?

我的C套接字编程函数中connect不成功

linux - 使用 perl 确定 IPv4 数据包的源端口

android - Android 应用程序监听传入 TCP/IP 消息的最佳方式是什么?