ソリューション構成ごとにconfigファイルを作成する

.NetFrameworkのソリューション構成ごとにweb.configを作成し、そのconfigファイルの内容を反映させる。
ちょっと、つまずいたのでメモしておく。
(アクション)
1. 新しいソリューション構成を作成する。
2. web.configを作成する。
3. 変換構文の作成
4. .csprojファイルの編集

1. 新しいソリューション構成の作成
Visual Studioの「ビルド」→「構成マネージャー」→「アクティブソリューション構成」→「新規作成」
で、新しいソリューション構成を作成する。












2. web.configの作成
Web.configを右クリックして「Config変換を追加」をクリックすると、先ほど追加した新しいソリューション構成のWeb.configが追加される。















3. 変換構文の作成
前回のブログを参考に作成する。
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <connectionStrings>
        <add name="ConnectionString"
          connectionString="Data Source=192.168.44.10.1\SQLEXPRESS;Initial Catalog=AWS_DB;;Connect Timeout=60;Persist Security Info=False; User ID=hoge;Password=1234"
          xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    </connectionStrings>
    <appSettings>
        <add key="Environment" value="LOCAL-AWS" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
    </appSettings>

    <system.web>
        <!-- Disable Debug Mode -->
        <compilation xdt:Transform="RemoveAttributes(debug)" />
    </system.web>
</configuration>


4. .csprojファイルの編集
以下、を.csprojファイルの最後に追加する。
これが重要。
この記述を追加しないと、追加したWeb.configが適用されない。
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\WebApplications\Microsoft.WebApplication.targets" />
<Target Name="BeforeBuild" Condition="'$(PublishProfileName)' == '' And '$(WebPublishProfileFile)' == ''">
    <TransformXml Source="Web.config" Transform="Web.$(Configuration).config" Destination="Web.config" />
</Target>

これでOK。

コメント

このブログの人気の投稿

C++の古いプロジェクトのビルドでerror MIDL2311 : statements outside library block are illegal in mktyplib compatability mode

web.config debug/releaseの内容を変換構文を使って切り替える