1. 思路 :增加一个参数接收端口
public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { //默认端口号5000 if (args.Length > 1 && args[0] == "-p") { string portStr = args[1]; if (Regex.IsMatch(portStr, @"^d*$")) { webBuilder.UseUrls($"http://*:{portStr}"); } } else { webBuilder.UseUrls($"http://*:5000"); } webBuilder.UseStartup<Startup>(); }); }
使用方法:dotnet xxx.dll -p 端口号