为dotnet应用指定启动的端口

默认的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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容