appsettings.Production.json具有来自appsettings.json之后的值

时间:2019-07-06 23:26:17

标签: c# asp.net asp.net-mvc asp.net-core

我在appsettings.json和appsettings.Production.json中具有不同的连接字符串,但是在发布到计算机上的IIS文件夹后,我在appsettings.Production.json中具有与appsettings.json中相同的连接字符串

我的Program.cs有

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>            
            WebHost.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    var env = hostingContext.HostingEnvironment;

                    using (var f = System.IO.File.AppendText(@"C:\temp\log.txt"))
                    {
                        f.WriteLine(env.EnvironmentName);//for Debug, it outputs "Production" (without quotes)
                    }

                    config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
                    config.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: false, reloadOnChange: true);
                })
                .UseStartup<Startup>()
                .UseNLog();

appsettings.json

{
//something is omitted
  "ConnectionStrings": {
    "Northwind": "Data Source=(localdb)\\mssqllocaldb;Initial Catalog=Northwind;Integrated Security=True",
    "ApplicationIdentityDbContextConnection": "Server=(localdb)\\mssqllocaldb;Database=OlegTarusovIdentity;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}

app.Production.json

{
  "ConnectionStrings": {
    "Northwind": "\"Data Source=(localdb)\\\\mssqllocaldb;Initial Catalog=Northwind;User Id=sa;Password=Password123",
    "ApplicationIdentityDbContextConnection": "Server=(localdb)\\mssqllocaldb;Database=OlegTarusovIdentity;User Id=sa;Password=Password123;MultipleActiveResultSets=true"
  }
}

我有CustomProfile.pubxml:

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>MSDeploy</WebPublishMethod>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish>http://olegtarusov.com</SiteUrlToLaunchAfterPublish>
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <ProjectGuid>917b0f52-135a-4943-991f-12f35fa4a9c6</ProjectGuid>
    <SelfContained>false</SelfContained>
    <_IsPortable>true</_IsPortable>
    <MSDeployServiceURL>localhost</MSDeployServiceURL>
    <DeployIisAppPath>OlegTarusov</DeployIisAppPath>
    <RemoteSitePhysicalPath />
    <SkipExtraFilesOnServer>True</SkipExtraFilesOnServer>
    <MSDeployPublishMethod>InProc</MSDeployPublishMethod>
    <EnableMSDeployBackup>False</EnableMSDeployBackup>
    <UserName />
    <_SavePWD>False</_SavePWD>
  </PropertyGroup>
</Project>

和launchSetting.json

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:50806",
      "sslPort": 44348
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Oleg_Tarusov": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      },
      "applicationUrl": "https://localhost:5001;http://localhost:5000"
    }
  }
}

发布后,我在IIS文件夹中有来自appsettings.json的值的appsettings.Production.json:

{
  "ConnectionStrings": {
    "Northwind": "Data Source=(localdb)\\mssqllocaldb;Initial Catalog=Northwind;Integrated Security=True",
    "ApplicationIdentityDbContextConnection": "Server=(localdb)\\mssqllocaldb;Database=OlegTarusovIdentity;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}

我希望像在Visual Studio中的项目一样具有appsettings.Production.json。 为什么我的appsettings.Production.json被appsetting.json覆盖?

1 个答案:

答案 0 :(得分:0)

我发现appsettings.Production.json从CustomProfile.pubxml.user获取值

在配置配置文件的第二步(上下文菜单中的“发布..”)中,您可以设置数据库的连接字符串。 在这里,我有与appsetting.json中一样的连接字符串。