如何在运行.NET 7 Web API时,防止appsettings.json中的值在本地主机上被覆盖?

huangapple go评论73阅读模式
英文:

How can I prevent appsettings.json values from being overridden when running on localhost in .NET 7 Web API?

问题

我帮你翻译以下部分:

"Localhost"配置值在本地主机运行时应保持不变。我已经为其他环境添加了单独的appsettings.{env}.json文件。我只想在开发环境中使用appsettings.Development.json文件,而不是在本地主机上使用。

我需要创建单独的appsettings.Localhost.json文件吗?或者我可以像下面这样创建新的配置文件(Localhost)?请建议。

"Localhost": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7055;http://localhost:5065",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Localhost"
      }
    }

以下是我的 Program.cs 文件:

var builder = WebApplication.CreateBuilder(args);
{
    // 环境配置
    var configuration = builder.Configuration;

    var env = builder.Environment;

    configuration
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);

    builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblyContaining<Program>());
    builder.Services.AddHttpClient();

    builder.Services.AddApplication();
    builder.Services.AddInfrastructure(builder.Configuration);

    builder.Services.AddHelpers();

    builder.Services.AddControllers()
       // 为控制器配置 JSON 序列化选项。
       .AddJsonOptions(options =>
        {
            options.JsonSerializerOptions.PropertyNamingPolicy = null;
        });

    builder.Services.Configure<ApiBehaviorOptions>(options =>
    {
        options.SuppressModelStateInvalidFilter = true;
    });

} 

请注意,我已经将代码部分保持不变,只翻译了您提供的文本。

英文:

I want the configuration values from appsettings.json to remain unchanged when running on localhost. I have already added separate appsettings.{env}.json files to other environments. I want to use appsettings.Development.json file only for Dev environment not for localhost.

Do i need to create separate appsettings.Localhost.json file? Or Can I create new profile (Localhost) like below? Please suggest.

&quot;Localhost&quot;: {
      &quot;commandName&quot;: &quot;Project&quot;,
      &quot;dotnetRunMessages&quot;: true,
      &quot;launchBrowser&quot;: true,
      &quot;applicationUrl&quot;: &quot;https://localhost:7055;http://localhost:5065&quot;,
      &quot;environmentVariables&quot;: {
        &quot;ASPNETCORE_ENVIRONMENT&quot;: &quot;Localhost&quot;
      }
    }

Below is my Program.cs

var builder = WebApplication.CreateBuilder(args);
{
    // Environment configuration
    var configuration = builder.Configuration;

    var env = builder.Environment;

    configuration
        .AddJsonFile(&quot;appsettings.json&quot;, optional: false, reloadOnChange: true)
        .AddJsonFile($&quot;appsettings.{env.EnvironmentName}.json&quot;, optional: true, reloadOnChange: true);


    builder.Services.AddMediatR(cfg =&gt; cfg.RegisterServicesFromAssemblyContaining&lt;Program&gt;());
    builder.Services.AddHttpClient();

    builder.Services.AddApplication();
    builder.Services.AddInfrastructure(builder.Configuration);

    builder.Services.AddHelpers();

    builder.Services.AddControllers()
       // Configures the JSON serialization options for controllers.
       .AddJsonOptions(options =&gt;
        {
            options.JsonSerializerOptions.PropertyNamingPolicy = null;
        });

    builder.Services.Configure&lt;ApiBehaviorOptions&gt;(options =&gt;
    {
        options.SuppressModelStateInvalidFilter = true;
    });

} 

答案1

得分: 1

我在我的一侧有一个测试,我在 appsettings.json 中添加了一个配置。当我将启动设置更改为 Localhost 时,它会读取 appsettings.Localhost.json 中的变量。

如何在运行.NET 7 Web API时,防止appsettings.json中的值在本地主机上被覆盖?

如何在运行.NET 7 Web API时,防止appsettings.json中的值在本地主机上被覆盖?

英文:

I have a test in my side, I add a configuration in appsettings.json. When I changed the launch settings to Localhost, then it would read variable in appsettings.Localhost.json

如何在运行.NET 7 Web API时,防止appsettings.json中的值在本地主机上被覆盖?

如何在运行.NET 7 Web API时,防止appsettings.json中的值在本地主机上被覆盖?

huangapple
  • 本文由 发表于 2023年5月20日 21:29:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定