为dotnet应用指定启动的端口

2022-01-18  本文已影响0人  黑山老雕

默认的dotnet webapp启动的端口是5000和5001,有5种方式可以自定义
5 ways to set the URLs for an ASP.NET Core app (andrewlock.net)

用代码

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

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

我喜欢参数的用法:

dotnet run --urls "http://localhost:5100;https://localhost:5101"
在linux下,可以直接在调用的可执行文件后加这个参数也可以 ./dotnet6test.Server --urls "http://localhost:5100;https://localhost:5101" 也可以

还有环境变量:

You can set environment variables in the usual way based on your environment. For example, using the command line:

setx ASPNETCORE_URLS "http://localhost:5001"
using powershell

$Env: ASPNETCORE_URLS = "http://localhost:5001"
or using bash:

export ASPNETCORE_URLS="http://localhost:5001;https://localhost:5002"

官方文档也可以参考一下这个:Configure endpoints for the ASP.NET Core Kestrel web server | Microsoft Docs

上一篇下一篇

猜你喜欢

热点阅读