為dotnet應(yīng)用指定啟動(dòng)的端口

默認(rèn)的dotnet webapp啟動(dòng)的端口是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");
            });
}

我喜歡參數(shù)的用法:

dotnet run --urls "http://localhost:5100;https://localhost:5101"
在linux下,可以直接在調(diào)用的可執(zhí)行文件后加這個(gè)參數(shù)也可以 ./dotnet6test.Server --urls "http://localhost:5100;https://localhost:5101" 也可以

還有環(huán)境變量:

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"

官方文檔也可以參考一下這個(gè):Configure endpoints for the ASP.NET Core Kestrel web server | Microsoft Docs

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容