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+ 浏览量

启动您的职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.