当前位置 博文首页 > SpringLeee:5种设置ASP.NET Core应用程序URL的方法

    SpringLeee:5种设置ASP.NET Core应用程序URL的方法

    作者:SpringLeee 时间:2021-02-14 10:29

    默认情况下,ASP.NET Core应用程序监听以下URL:

    • http://localhost:5000
    • https://localhost:5001

    在这篇文章中,我展示了5种不同的方式来更改您的应用程序监听的URL。

    • 在Program.cs中使用 UseUrls()
    • 环境变量 - 使用DOTNET_URLS或者 ASPNETCORE_URLS
    • 命令行参数 - 设置命令行参数--urls
    • launchSettings.json - 设置 applicationUrl 属性
    • KestrelServerOptions.Listen() - 使用 Listen() 手动使用配置Kestrel服务器的地址

    我将在下面更详细地介绍每个选项。

    UseUrls()

    设置绑定URL的第一个也是最简单的方法,在配置IWebHostBuilder的时候使用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");
                });
    }
    

    环境变量

    .NET Core使用两种类型的配置:

    • DOTNET_URLS
    • ASPNETCORE_URLS

    如果您同时设置了这两个环境变量,那么ASPNETCORE_URLS参数优先。

    您可以用不同的方式设置环境变量。例如,使用命令行:

    setx ASPNETCORE_URLS "http://localhost:5001"
    

    使用powershell

    $Env: ASPNETCORE_URLS = "http://localhost:5001"
    

    使用bash:

    export ASPNETCORE_URLS="http://localhost:5001;https://localhost:5002"
    

    如上所示,您还可以通过使用分号分隔多个地址来传递多个地址以进行监听(使用HTTP或HTTPS)。

    命令行参数

    设置主机配置值的另一种方法是使用命令行。如果设置了命令行参数,那么会覆盖环境变量的值, 只需使用--urls参数:

    dotnet run --urls "http://localhost:5100"
    

    和上面一样,您可以通过使用分号将多个URL分开来设置多个URL:

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

    环境变量和命令行参数可能是在生产环境中为应用程序设置URL的最常见方法,但是它们对于本地开发来说有点麻烦。通常使用launchSettings.json会更容易。

    launchSettings.json

    大多数 .NET项目模板在Properties文件夹中都包含launchSettings.json文件,这个文件包含了启动.NET Core应用程序的各种配置文件。

    {
      "iisSettings": {
        "windowsAuthentication": false, 
        "anonymousAuthentication": true, 
        "iisExpress": {
          "applicationUrl": "http://localhost:38327",
          "sslPort": 44310
        }
      },
      "profiles": {
        "IIS Express": {
          "commandName": "IISExpress",
          "launchBrowser": true,
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        },
        "TestApp": {
          "commandName": "Project",
          "launchBrowser": true,
          "applicationUrl": "https://localhost:5001;http://localhost:5000",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        }
      }
    }
    

    launchSettings.json还提供了environmentVariables参数,您可以用它来设置环境变量,就像上面这样,然后我们可以选择不同的启动类型:

    KestrelServerOptions.Listen

    默认情况下,几乎所有的.NET Core应用程序都配置了Kestrel,如果需要,您可以手动配置Kestrel的端点,也可以配置KestrelServerOptions。

    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.UseKestrel(opts =>
                    {
                        // Bind directly to a socket handle or Unix socket
                        // opts.ListenHandle(123554);
                        // opts.ListenUnixSocket("/tmp/kestrel-test.sock");
                        opts.Listen(IPAddress.Loopback, port: 5002);
                        opts.ListenAnyIP(5003);
                        opts.ListenLocalhost(5004, opts => opts.UseHttps());
                        opts.ListenLocalhost(5005, opts => opts.UseHttps());
                    });
    
                });
    }
    

    我个人没有以这种方式在Kestrel中设置监听端点,但是很高兴知道可以根据需要完全控制Kestrel。

    总结

    在这篇文章中,我展示了五种不同的方式来设置应用程序监听的URL。UseUrls()是最简单的一种,但通常不适合在生产中使用, launchSettings.json文件是在开发环境中设置的URL是非常有用的。 在生产中我们通常使用命令行参数--urls或者环境变量ASPNETCORE_URLS和DOTNET_URLS, 希望对您有帮助。

    原文链接: https://andrewlock.net/5-ways-to-set-the-urls-for-an-aspnetcore-app/

    最后

    欢迎扫码关注我们的公众号 【全球技术精选】,专注国外优秀博客的翻译和开源项目分享,也可以添加QQ群 897216102

    bk