ASP.NET Core配置监听URLs的六种方式

有以下六种监听方式:

  1. UseUrls(),在Program.cs配置程序监听的URLs,硬编码配置监听方式,适合调试使用
  2. lanuchSettings.json,使用applicationUrls属性来配置URLs,适合调试使用
  3. 环境变量,使用DOTNET_URLS或者ASPNETCORE_URLS配置URLs
  4. 命令行参数,当使用命令行启动应用时,使用--urls参数指定URLs
  5. KestrelServerOptions.Listen(),使用Listen()方法手动配置Kestral服务器监听的地址
  6. 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://mydomain, http://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"
      }
    }
  }
}
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值