Blazor 项目的 app.config 解决方案是什么?

What is the app.config solution for a Blazor project?

Blazor 应用程序的 app.config / web.config 解决方案是什么。 我几乎可以肯定这对于 Blazor 服务器端项目是可行的。但更深入地想知道如何为客户端项目处理配置。

本质上,我需要知道应用 运行 处于何种环境(例如开发、测试、生产)。

What is the configuration story for Blazor?

一样Configuration as for ASP.NET core. You basically have an appsettings.environment.json file per environment and use the build in IConfiguration interface to bind your settings to classes。由于此类设置以 json 格式存储:

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "Serilog": {
    "LevelSwitches": { "$controlSwitch": "Debug" },
    "MinimumLevel": { "ControlledBy": "$controlSwitch" },
    "WriteTo": [
...

有多少种环境以及如何称呼它们,由您决定。 这是一个例子:

在运行时,环境会从 OS environment variable ASPNETCORE_ENVIRONMENT 的值中扣除。出于开发目的,您可以创建配置文件并像这样在 launchSettings.json 中设置 ASPNETCORE_ENVIRONMENT

"profiles": {
    "Development": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
}

部署或开始执行 Blazor 应用程序后,将使用 server-side 和 client-side 应用程序的主机 ASPNETCORE_ENVIRONMENT。请记住,即使它是 client-side / SPA 应用程序,它仍然托管在服务器上。 同样对于 client-side Blazor 应用程序,appsettings 不会被传送到客户端,您不希望向您的客户端公开您的连接字符串和其他敏感设置,对吗?

作为旁注,如果您在 IIS 中托管您的 Blazor 应用程序(作为反向代理),将创建一个 web.config 文件,用于指示 IIS 如何启动应用程序、传递哪些参数以及其他一些事情的目的。