おのれ鍋奉行が!

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

最終更新:

lmes2

- view
メンバー限定 登録/ログイン

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


概要

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

元ネタ:

前提条件


手順

custom.configというファイル名で以下を追加する。
<?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」を指定
<appSettings file="custom.config"/>
 

default.aspx.vbで以下のように取得
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内の記述も取得できる。

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