我在project.json
中添加了以下部分。
"commands": {
"run": "run server.urls=http://localhost:8082",
"web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.Kestrel --server.urls http://localhost:8082",
"weblistener": "Microsoft.AspNet.Hosting --server WebListener --server.urls http://localhost:8082"
},
但是,当使用http://localhost:5000运行它时,它仍然显示“正在收听:dotnet myapp.dll
”?
顺便说一下,来自其他机器的客户能否访问该服务?
是的,如果您绑定任何外部IP地址,这将可从其他计算机访问。例如绑定到http://*:80
。请注意,绑定到http://localhost:80
只会绑定在127.0.0.1接口上,因此无法从其他计算机访问。
Visual Studio正在覆盖您的端口。您可以更改VS端口编辑此文件Properties\launchSettings.json
或者通过代码设置它:
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseUrls("http://*:80") // <-----
.Build();
host.Run();
here提供了使用外部配置文件的分步指南。
在.UseUrls("http://*:80")
中使用以下一行代码Program.cs
因此改变.UseStartup<Startup>()
至
.UseStartup<Startup>()
.UseUrls("http://*:80")
我们可以使用此命令通过Windows Powershell运行我们的主机项目,而不使用IIS和Visual Studio在单独的端口上。 krestel Web服务器的默认值为5001
$env:ASPNETCORE_URLS="http://localhost:22742" ; dotnet run
你也可以像这样编码
IConfiguration config = new ConfigurationBuilder()
.AddCommandLine(args)
.Build();
var host = new WebHostBuilder()
.UseConfiguration(config)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build();
并通过命令行设置应用程序:dotnet run --server.urls http:// *:5555
转到program.cs文件添加UseUrs方法来设置你的url,确保你不使用保留的url或端口
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
// params string[] urls
.UseUrls(urls: "http://localhost:10000")
.Build();
}