Webアプリケーションの発行時に切り替える方法のため、Visual Studioでデバッグする場合などではWeb.configの変更は反映されませんので注意してください。
<?xml version="1.0" encoding="utf-8"?>
<!--
ASP.NET アプリケーションの構成方法の詳細については、
https://go.microsoft.com/fwlink/?LinkId=169433 を参照してください
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.8"/>
<httpRuntime targetFramework="4.8"/>
</system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/>
</compilers>
</system.codedom>
</configuration>
<?xml version="1.0" encoding="utf-8"?>
<!-- web.config 変換の使用方法の詳細については、https://go.microsoft.com/fwlink/?LinkId=125889 を参照してください -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--
次の例では、"MyDB" という値を持つ "name" 属性が "Match" ロケーターで
見つかった場合にのみ、"SetAttributes" 変換によって "connectionString" の
値が "ReleaseSQLServer" を使用するように変更されます。
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
-->
<system.web>
<!--
次の例では、web.config ファイルの <customErrors> セクション全体が
"Replace" 変換によって置き換えられます。
<system.web> ノードには customErrors セクションが 1 つしかないため、
"xdt:Locator" 属性を使用する必要はありません。
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
>
-->
</system.web>
</configuration>
<?xml version="1.0" encoding="utf-8"?>
<!-- web.config 変換の使用方法の詳細については、https://go.microsoft.com/fwlink/?LinkId=125889 を参照してください -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--
次の例では、"MyDB" という値を持つ "name" 属性が "Match" ロケーターで
見つかった場合にのみ、"SetAttributes" 変換によって "connectionString" の
値が "ReleaseSQLServer" を使用するように変更されます。
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
-->
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
<!--
次の例では、web.config ファイルの <customErrors> セクション全体が
"Replace" 変換によって置き換えられます。
<system.web> ノードには customErrors セクションが 1 つしかないため、
"xdt:Locator" 属性を使用する必要はありません。
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
-->
</system.web>
</configuration>
appSettings
タグ add
タグを記述します。<?xml version="1.0" encoding="utf-8"?>
<!--
ASP.NET アプリケーションの構成方法の詳細については、
https://go.microsoft.com/fwlink/?LinkId=169433 を参照してください
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.8"/>
<httpRuntime targetFramework="4.8"/>
</system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/>
</compilers>
</system.codedom>
<appSettings>
<add key="AppInfo" value="初期値です。"/>
</appSettings>
</configuration>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DisplayInfo.aspx.cs" Inherits="WebConfigDemo.DisplayInfo" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
namespace WebConfigDemo
{
public partial class DisplayInfo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string value = ConfigurationManager.AppSettings["AppInfo"];
Label1.Text = value;
}
}
}
ConfigurationManager.AppSettings
を利用してWeb.configファイルから値を取得して、Label1に表示します。DisplayInfo.aspx
を開きます。<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.web>
</system.web>
<appSettings>
<add key="AppInfo" value="デバッグ版の値です。" xdt:Locator="Match(key)" xdt:Transform="Replace"/>
</appSettings>
</configuration>
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
</system.web>
<appSettings>
<add key="AppInfo" value="リリース版の値です。" xdt:Locator="Match(key)" xdt:Transform="Replace"/>
</appSettings>
</configuration>
xdt:Locator="Match(key)"
により、Web.config の 同じ位置のタグで key属性の値が一致するタグに対して処理を実行します。
xdt:Transform="Replace"
により、タグの値の置換を実行します。 <add key="AppInfo" value="デバッグ版の値です。" xdt:Locator="Match(key)" xdt:Transform="Replace"/>
<?xml version="1.0" encoding="utf-8"?>
<!--
ASP.NET アプリケーションの構成方法の詳細については、
https://go.microsoft.com/fwlink/?LinkId=169433 を参照してください
-->
<configuration>
<system.web>
<compilation targetFramework="4.8" />
<httpRuntime targetFramework="4.8" />
</system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
</compilers>
</system.codedom>
<appSettings>
<add key="AppInfo" value="リリース版の値です。" />
</appSettings>
</configuration>
<!--ProjectGuid: FF9EA0A9-EA3B-4DAD-B5D8-454F8C091AF8-->
<?xml version="1.0" encoding="utf-8"?>
<!--
ASP.NET アプリケーションの構成方法の詳細については、
https://go.microsoft.com/fwlink/?LinkId=169433 を参照してください
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.8" />
<httpRuntime targetFramework="4.8" />
</system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
</compilers>
</system.codedom>
<appSettings>
<add key="AppInfo" value="デバッグ版の値です。" />
</appSettings>
</configuration>
<!--ProjectGuid: FF9EA0A9-EA3B-4DAD-B5D8-454F8C091AF8-->