• atwiki
  • おのれ鍋奉行が!
  • web.configの設定記述を、外部ファイルに定義し、プログラム部分から取得するの編集履歴ソース
「web.configの設定記述を、外部ファイルに定義し、プログラム部分から取得する」の編集履歴(バックアップ)一覧に戻る

web.configの設定記述を、外部ファイルに定義し、プログラム部分から取得する - (2008/01/31 (木) 10:15:10) のソース

*&this_page()

**概要
1.web.configの設定記述を、外部ファイルに定義する。
2.プログラム部分から1を取得する。

元ネタ:
-[[DOBON.NET / 「アプリケーション構成ファイル」を使用して設定を読み込む>>http://dobon.net/vb/dotnet/programing/appconfigfile.html]]

**前提条件
-[[新しいwebサイトの作成]]

**手順
custom.configというファイル名で以下を追加する。
#highlight(xml){{
<?xml version="1.0" encoding="UTF-8" ?>
<appSettings>
  <add key="ConnectString" value="Provider=SQLOLEDB;Persist Security Info=True;
           Password=sa;User ID=sa;Initial Catalog=netinsider;Data Source=(local)" />
</appSettings>
}}

web.config中の「appSettings」に「file」を指定
#highlight(xml){{
<appSettings file="custom.config"/>
}}

default.aspx.vbで以下のように取得
#highlight(vb.net){{
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.Write(System.Configuration.ConfigurationManager.AppSettings("ConnectString"))
    End Sub
End Class
}}

実行すると以下のような感じ。
※ここでは外部ファイルにしているが、web.config内の記述も取得できる。

#img(114.jpg)
記事メニュー
目安箱バナー