C# ASP.NET Core 中有哪些不同的 JSON 檔案?


ASP.NET Core 重新架構了之前的 ASP.NET 版本,之前的版本配置依賴於 System.Configuration 和 web.config 檔案中的 xml 配置。在 ASP.NET Core 中,有一種新的簡便方法來宣告和訪問解決方案的全域性設定、專案特定設定、客戶端特定設定等。新的配置模型可以使用 XML、INI 和 JSON 檔案。

ASP.NET Core 中不同的 JSON 配置檔案 ASP.NET Core 中主要有 6 個 JSON 配置檔案。

global.json
launchsettings.json
appsettings.json
bundleconfig.json
project.json
bower.json

global.json

示例

You can define the solution level settings in global.json file.{
   "projects": [ "src", "test" ],
   "sdk": {
      "version": "1.0.0-preview2-003121"
   }
}

projects - projects 屬性定義瞭解決方案原始碼的位置。它指定了解決方案中專案的兩個位置:src 和 test。src 包含實際應用程式,test 包含任何測試。

launchsettings.json

在 launchsettings.json 檔案中,您可以定義與每個配置檔案關聯的專案特定設定,Visual Studio 將配置為啟動應用程式,包括應使用的任何環境變數。您可以為專案的編譯和特定配置檔案的除錯定義框架。

{
   "iisSettings": {
      "windowsAuthentication": false,
      "anonymousAuthentication": true,
      "iisExpress": {
         "applicationUrl": "https://:50944/",
            "sslPort": 0
      }
   },
   "profiles": {
      "IIS Express": {
         "commandName": "IISExpress",
         "launchBrowser": true,
         "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
         }
      },
      "ASPCoreMVCHelloWorld": {
         "commandName": "Project",
         "launchBrowser": true,
         "launchUrl": "https://:5000",
         "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
         },
         "kestrel": {
            "commandName": "kestrel",
            "sdkVersion": "dnx-clr-win-x86.1.0.0-preview2-003121"
         }
      }
   }
}

您可以透過右鍵單擊專案,然後選擇屬性來更改每個配置檔案的設定。

appsettings.json

ASP.NET 將應用程式配置設定儲存在 Web.config 中。ASP.NET Core 使用 AppSettings.json 來儲存自定義應用程式設定、資料庫連線字串、日誌記錄等。以下是 Appsettings.json 的示例:

{
   "ApplicationInsights": {
      "InstrumentationKey": ""
   },
   "Logging": {
      "IncludeScopes": false,
      "LogLevel": {
         "Default": "Debug",
         "System": "Information",
         "Microsoft": "Information"
      }
   }
}

bundleconfig.json

您可以定義專案的捆綁和壓縮配置。

[
   {
      "outputFileName": "wwwroot/css/site.min.css",
      // An array of relative input file paths. Globbing patterns supported
      "inputFiles": [
         "wwwroot/css/site.css"
      ]
   },
   {
      "outputFileName": "wwwroot/js/site.min.js",
      "inputFiles": [
         "wwwroot/js/site.js"
      ],
      // Optionally specify minification options
      "minify": {
         "enabled": true,
         "renameLocals": true
      },
      // Optinally generate .map file
      "sourceMap": false
   }
]

project.json

Asp.net Core 使用 Project.JSON 檔案來儲存所有專案級別的配置設定。Project.json 檔案以 JSON 格式儲存配置資訊。

{
   "dependencies": {
      "Microsoft.NETCore.App": {
         "version": "1.0.0",
         "type": "platform"
      },
      "Microsoft.ApplicationInsights.AspNetCore": "1.0.0",
      "Microsoft.AspNetCore.Diagnostics": "1.0.0",
      "Microsoft.AspNetCore.Mvc": "1.0.0",
      "Microsoft.AspNetCore.Razor.Tools": {
         "version": "1.0.0-preview2-final",
         "type": "build"
      },
      "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
      "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
      "Microsoft.AspNetCore.StaticFiles": "1.0.0",
      "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
      "Microsoft.Extensions.Configuration.Json": "1.0.0",
      "Microsoft.Extensions.Logging": "1.0.0",
      "Microsoft.Extensions.Logging.Console": "1.0.0",
      "Microsoft.Extensions.Logging.Debug": "1.0.0",
      "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
      "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0"
   }
}

bower.json

Bower 是一個 Web 包管理器。Bower 管理包含 HTML、CSS、JavaScript、字型甚至影像檔案的元件。Bower 安裝您需要的包及其依賴項的正確版本。

更新於:2020年9月25日

6K+ 瀏覽量

啟動您的職業生涯

透過完成課程獲得認證

開始學習
廣告