简体   繁体   English

Asp.Net Core Web API 应用程序:如何更改侦听地址?

[英]Asp.Net Core Web API app: how to change listening address?

I write simple Asp.Net Core WebAPI 2.0 application, it works on my local machine.我编写了简单的 Asp.Net Core WebAPI 2.0 应用程序,它可以在我的本地机器上运行。 But i want to deploy it to server.但我想将它部署到服务器。 So, i do it.所以,我这样做。

My system:我的系统:

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

But, when app starts it writes:但是,当应用程序启动时,它写道:

Now listening on:http://localhost:53115

When i try to get values from it:当我尝试从中获取值时:

  http://id_address:53115/api/values

And i can not get response.我无法得到回应。 In Postman:在邮递员中:

 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

What i should do?我该做什么? Can you tell me how to fix that?你能告诉我如何解决这个问题吗?

I do not know where to start looking for.我不知道从哪里开始寻找。

Thank you!谢谢!

I'm using .NETCore 2.1 prev, so I haven't been able to test it myself but if I believe that https://www.billbogaiv.com/posts/setting-aspnet-host-address-in-net-core-2 , adding .UseUrls(urls: "http://*:5000") might be instructing Kestrel to listen to port 5000 and not only on localhost, so it should work also on a remote server.我使用的是 .NETCore 2.1 prev,所以我无法自己测试,但如果我相信https://www.billbogaiv.com/posts/setting-aspnet-host-address-in-net-core -2 ,添加.UseUrls(urls: "http://*:5000")可能会指示 Kestrel 侦听端口 5000 而不仅仅是在本地主机上,因此它也应该在远程服务器上工作。

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

This is configured by Server urls host configuration这是由Server urls主机配置配置的

Indicates the IP addresses or host addresses with ports and protocols that the server should listen on for requests.指示服务器应侦听请求的带有端口和协议的 IP 地址或主机地址。
Key : urls关键:网址
Type : string类型:字符串
Default : http://localhost:5000默认值http://localhost:5000
Set using : UseUrls设置使用:UseUrls
Environment variable: ASPNETCORE_URLS环境变量:ASPNETCORE_URLS

Set to a semicolon-separated (;) list of URL prefixes to which the server should respond.设置为服务器应响应的 URL 前缀的分号分隔 (;) 列表。


It is also possible to set URL using command line arguments.也可以使用命令行参数设置 URL。 For example:例如:

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

but this doesn't work out of the box for old versions of ASP.NET Core ( depends on whether this fix applied to used version or not ).但这不适用于旧版本的 ASP.NET Core(取决于此修复程序是否适用于使用过的版本)。

A workaround for old versions based on fact that you always can set host settings directly via .UseConfiguration method:基于以下事实的旧版本解决方法:您始终可以通过.UseConfiguration方法直接设置主机设置:

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

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

Note, the same idea may be used to read setting value from any other configuration source, like configuration file for example.请注意,相同的想法可用于从任何其他配置源读取设置值,例如配置文件

Try the below, it worked for me.试试下面的,它对我有用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM