ASP.NET Core配置監聽URLs的六種方式


 有以下六種監聽方式:

UseUrls(),在Program.cs配置程序監聽的URLs,硬編碼配置監聽方式,適合調試使用

lanuchSettings.json,使用applicationUrls屬性來配置URLs,發布后無此文件,適合調試使用

環境變量,使用DOTNET_URLS或者ASPNETCORE_URLS配置URLs

命令行參數,當使用命令行啟動應用時,使用--urls參數指定URLs

KestrelServerOptions.Listen(),使用Listen()方法手動配置Kestral服務器監聽的地址(此方式可以自己添加證書)

appsettings.json中添加Urls節點--"Urls": "http://*:9991;https://*:9993"

 

URL格式:

  • localhost:http://localhost:5000
  • 指定ip:在你機器上可用的指定IP地址(例如http://192.168.8.31:5005)
  • 任何ip:使用"任何"IP地址(例如http://*:6264

注意,針對"任何"IP地址的格式 - 你不一定必須使用*,你可以使用任何字符,只要不是IP地址或者localhost, 這意味着你可以使用http://*http://+http://mydomainhttp://example.org. 以上所有字符串都具有相同的行為,可以監聽任何IP地址。如果你想僅處理來自單一主機名的請求,你需要額外配置主機過濾。

 

UseUrls():

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_URLS
  • ASPNETCORE_URLS

如果你同時使用2種環境變量,系統會優先使用ASPNETCORE_URLS中定義的參數

 

命令行:

dotnet run --urls "http://localhost:5100"
dotnet run --urls "http://localhost:5100;https://localhost:5101"

 

KestrelServerOptions.Listen():

幾乎所有的ASP.NET Core應用默認都會使用Kestrel服務器。如果你想的話,你可以手動配置Kestrel服務器節點,或者使用IConfiguration配置KestrelServerOptions

        /// <summary>
        /// 添加服務器啟動配置信息
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddKestrelServer(this IServiceCollection services)
        {
            //https://www.humankode.com/asp-net-core/develop-locally-with-https-self-signed-certificates-and-asp-net-core
            services.Configure<KestrelServerOptions>(options =>
            {
                options.AllowSynchronousIO = true;//允許同步IO,AddWebMarkupMin這個插件需要用到。
#if DEBUG
                options.AddServerHeader = true;
#else
                options.AddServerHeader = false;
#endif
                //取出configuration對象
                IConfiguration configuration = (IConfiguration)options.ApplicationServices.GetService(typeof(IConfiguration));
                IConfigurationSection configurationSection = configuration.GetSection("Hosts");
                if (configurationSection.Exists())
                {
                    EndPoint endPoint = null;
                    Certificate certificate = null;
                    EndPoints enpPoints = configurationSection.Get<EndPoints>();
                    foreach (KeyValuePair<string, EndPoint> pair in enpPoints.Protocols)
                    {
                        endPoint = pair.Value;
                        certificate = endPoint.Certificate;
                        if (certificate == null)
                        {
                            options.Listen(System.Net.IPAddress.Parse(endPoint.Address), endPoint.Port);
                        }
                        else
                        {
                            options.Listen(System.Net.IPAddress.Parse(endPoint.Address), endPoint.Port,
                                opts =>
                                {
                                    opts.UseHttps(new System.Security.Cryptography.X509Certificates.X509Certificate2(certificate.FileName, certificate.Password));
                                });
                        }
                    }
                }
            });
            return services;
        }

url配置:

{
  "Hosts": {
    "Protocols": {
      "Http": {
        "Address": "0.0.0.0",
        "Port": "7777"
      }
    }
  }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM