5種設置ASP.NET Core應用程序URL的方法

默認狀況下,ASP.NET Core應用程序監聽如下URL:web

在這篇文章中,我展現了5種不一樣的方式來更改您的應用程序監聽的URL。shell

  • 在Program.cs中使用 UseUrls()
  • 環境變量 - 使用DOTNET_URLS或者 ASPNETCORE_URLS
  • 命令行參數 - 設置命令行參數--urls
  • launchSettings.json - 設置 applicationUrl 屬性
  • KestrelServerOptions.Listen() - 使用 Listen() 手動使用配置Kestrel服務器的地址

我將在下面更詳細地介紹每一個選項。json

UseUrls()

設置綁定URL的第一個也是最簡單的方法,在配置IWebHostBuilder的時候使用UseUrls()進行硬編碼。windows

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使用兩種類型的配置:bash

  • DOTNET_URLS
  • ASPNETCORE_URLS

若是您同時設置了這兩個環境變量,那麼ASPNETCORE_URLS參數優先。服務器

您能夠用不一樣的方式設置環境變量。例如,使用命令行:app

setx ASPNETCORE_URLS "http://localhost:5001"

使用powershellsocket

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

使用bash:oop

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

如上所示,您還能夠經過使用分號分隔多個地址來傳遞多個地址以進行監聽(使用HTTP或HTTPS)。ui

命令行參數

設置主機配置值的另外一種方法是使用命令行。若是設置了命令行參數,那麼會覆蓋環境變量的值, 只需使用--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

相關文章
相關標籤/搜索