Asp.net core 環境配置

參考:數據庫

在 ASP.NET Core 中使用多個環境json

ASP.NET Core 中的配置windows

在項目的 Properties\launchSettings.json中能夠配置多個環境api

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:49716/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "ApiCenter": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://localhost:5001/"
    },
    "ApiCenter-Test": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Staging"
      },
      "applicationUrl": "http://localhost:5002/"
    },
    "ApiCenter-Production": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      },
      "applicationUrl": "http://localhost:5003/"
    }
  }
}
View Code

在VS調試的時候能夠選擇使用哪一個app

對應的appsetting.jsonide

這樣就能夠在不一樣的環境使用不一樣的配置post

設置環境

Windows

cmd中執行ui

set ASPNETCORE_ENVIRONMENT=Development

這個只對當前cmd窗口有效,關閉後就失效了,要永久修改的話能夠設置環境變量spa

Linux

使用export 或者永久修改/etc/profile調試

 

接下來在發佈目錄執行就會使用配置的環境了

固然若是在源碼目錄執行的話只會使用第一個配置

 

 把上面兩個註釋再執行

 

經過命令參數配置

如原來的數據庫配置地址以下

"ConnectionStrings": {
    "DefaultConnection": "Server=10.202.202.245;Database=test;uid=sa;pwd=password"
  }

可使用命令更改

dotnet ApiCenter.dll /ConnectionStrings:DefaultConnection=Server=10.15.4.155;Database=postgres;uid=root;pwd=password
相關文章
相關標籤/搜索