拆分配置文件

时间:2017-03-04 23:30:37

标签: asp.net xml config

我正在尝试split up private information out of config files。 我发现了project type can't be a website。 但它仍然不适用于appsettings,对于连接字符串,它使用Debug构建版本,即使在Release模式下也是如此。

我的项目是authentication removed的默认Visual Studio单页Web应用程序。以下秘密文件:

**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中:

<?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:     

<!-- 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. 根据使用的构建配置,包括右侧机密文件中的连接字符串。
  2. 默认包含Web.setting.secrets文件中的应用程序设置。
  3. 根据使用的构建配置,从正确的机密文件中包含应用程序设置。

1 个答案:

答案 0 :(得分:1)

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

  • Secrets.config档案

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

您指的是发布配置的最后一个块应该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>

参考:Web.config Transformation Syntax for Web Application Project Deployment

Hth~