【问题标题】:appsettings.Production.json has values from appsettings.json afte publishingappsettings.Production.json 在发布后具有来自 appsettings.json 的值
【发布时间】:2019-07-07 13:50:07
【问题描述】:

我在 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.Production.json,其中包含来自 appsettings.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 覆盖了?

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-11-14
      • 1970-01-01
      • 2019-03-16
      • 2017-06-23
      • 1970-01-01
      • 1970-01-01
      • 2020-08-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多