13

我编写了简单的 Asp.Net Core WebAPI 2.0 应用程序,它可以在我的本地机器上运行。但我想将它部署到服务器。所以,我做到了。

我的系统:

Ubuntu 16.04.
Asp.Net WebAPI 2.0 (and dotnet version 2.1.105)

但是,当应用程序启动时,它会写道:

Now listening on:http://localhost:53115

当我尝试从中获取值时:

  http://id_address:53115/api/values

我无法得到回应。在邮递员中:

 Could not get any response
 There was an error connecting to 
 http://id_address:53115/api/values.
 Why this might have happened:
 The server couldn't send a response:
 Ensure that the backend is working properly
 Self-signed SSL certificates are being blocked:
 Fix this by turning off 'SSL certificate verification' in Settings > General
 Proxy configured incorrectly
 Ensure that proxy is configured correctly in Settings > Proxy
 Request timeout:
 Change request timeout in Settings > General

我该做什么?你能告诉我如何解决这个问题吗?

我不知道从哪里开始寻找。

谢谢!

4

3 回答 3

10

我正在使用 .NETCore 2.1 prev,所以我自己无法测试它,但如果我相信https://www.billbogaiv.com/posts/setting-aspnet-host-address-in-net-core -2,添加.UseUrls(urls: "http://*:5000")可能会指示 Kestrel 侦听端口 5000 而不仅仅是在本地主机上,因此它也应该在远程服务器上工作。

其他可能的解决方案,UseKestrel(..) https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore -2.1&tabs=aspnetcore2x使用IPAddress.Any而不是Loopback.

于 2018-04-30T09:54:43.353 回答
9

这是由Server urls主机配置配置的

指示服务器应侦听请求的 IP 地址或主机地址以及端口和协议。
:urls
类型:字符串
默认值http://localhost:5000
设置使用:UseUrls
环境变量:ASPNETCORE_URLS

设置为服务器应响应的 URL 前缀的分号分隔 (;) 列表。


也可以使用命令行参数设置 URL。例如:

dotnet 运行 --urls= http://0.0.0.0:5001

但这不适用于旧版本的 ASP.NET Core(取决于此修复程序是否适用于使用过的版本)。

旧版本的解决方法是基于您始终可以通过.UseConfiguration以下方法直接设置主机设置:

var config = new ConfigurationBuilder().AddCommandLine(args).Build();

return WebHost.CreateDefaultBuilder(args)
        .UseConfiguration(config)

请注意,同样的想法可以用于从任何其他配置源读取设置值,例如配置文件

于 2018-04-30T11:23:13.890 回答
-1

试试下面的,它对我有用。

设置 ASPNETCORE_SERVER.URLS=http://0.0.0.0:5000/

于 2020-07-28T09:55:26.750 回答