分割配置文件 [英] Splitting up config files

查看:60
本文介绍了分割配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从配置文件中分离出私人信息
我发现项目类型不能是网站
,但是对于设置仍然完全不起作用,对于连接字符串,即使在发布模式下,它也使用Debug构建版本。

I'm trying to split up private information out of config files. I've found that the project type can't be a website. But it still doesn't work for appsettings at all, for connection strings, it uses the Debug build version, even when on Release mode.

我的项目是默认的Visual Studio单页Web应用程序,其中删除了身份验证已删除。以下机密文件:

My project is a default Visual Studio Single Page Web Application with authentication removed. The following secrets files:

**Web.settings.secrets:**
<?xml version="1.0" encoding="utf-8"?>
<connectionStrings xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <add name="Default" providerName="Oracle.ManagedDataAccess.Client" connectionString="Release connectionString" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

**Web.settings.Release.secrets:**
<?xml version="1.0" encoding="utf-8" ?>
<appSettings xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <add name="setting" value="Release setting" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</appSettings>

**Web.connection.secrets:**
<?xml version="1.0" encoding="utf-8"?>
<connectionStrings>
    <add name="Default" providerName="Oracle.ManagedDataAccess.Client" connectionString="Debug connectionString" />
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApp-20170304072749.mdf;Initial Catalog=aspnet-WebApp-20170304072749;Integrated Security=True"
      providerName="System.Data.SqlClient" />
</connectionStrings>

**Web.connection.Release.secrets:**
<?xml version="1.0" encoding="utf-8"?>
<connectionStrings xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <add name="Default" providerName="Oracle.ManagedDataAccess.Client" connectionString="Release connectionString" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

Web.config和Web.Release.config在其connectionStrings和appSettings元素中引用这些文件。

Web.config:

The Web.config and Web.Release.config refer to these files in their connectionStrings and appSettings elements.
Web.config:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301879
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
    <connectionStrings configSource="Web.connection.secrets">
  </connectionStrings>
  <appSettings file="Web.settings.secrets">
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.webServer>
    <modules>
      <remove name="FormsAuthentication" />
    </modules>
  <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers></system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

Web.Release.config:

Web.Release.config:

<!-- For more information on using Web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=301874 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <!--
    In the example below, the "SetAttributes" transform will change the value of
    "connectionString" to use "ReleaseSQLServer" only when the "Match" locator
    finds an attribute "name" that has a value of "MyDB".

  -->
    <connectionStrings configSource="Web.connection.Release.secrets">
    </connectionStrings>
<appSettings file="Web.settings.Release.secrets">
</appSettings>
  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
    <!--
      In the example below, the "Replace" transform will replace the entire
      <customErrors> section of your Web.config file.
      Note that because there is only one customErrors section under the
      <system.web> node, there is no need to use the "xdt:Locator" attribute.

      <customErrors defaultRedirect="GenericError.htm"
        mode="RemoteOnly" xdt:Transform="Replace">
        <error statusCode="500" redirect="InternalError.htm"/>
      </customErrors>
    -->
  </system.web>
</configuration>

我真的想:


  1. 根据使用的构建配置,包含来自正确的secret文件的连接字符串。

  2. 默认情况下,包含来自Web.setting.secrets文件的应用程序设置。

  3. 根据使用的构建配置,包含来自正确机密文件的应用设置。


推荐答案

您的第一个块-您所说的单独设置文件不应具有XML声明或转换。示例:

Your first block - what you are referring to as the separate settings files shouldn't have XML declarations, nor transforms. Examples:


  • Secrets.config 文件

<add name="setting" value="Release setting" />
<add name="foo" value="bar" />


您所指的最后一个块当您的发布配置应该具有转换

Your last block, which you are referring to as your release config should have transforms:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
...

    <connectionStrings 
        configSource="Web.connection.Release.secrets.config" 
        xdt:Transform="Replace">
    </connectionStrings>

   <!-- AppSettings file location sample - leaves child elements intact-->
   <appSettings file="releasekeys.config" xdt:Transform="SetAttributes(file)"></appSettings>

REF:用于Web应用程序项目部署的Web.config转换语法

Hth 〜

这篇关于分割配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆