Config API
Config API 提供了快速存取 web.config
或 app.config
資料的方法,優化原先冗長的 ConfigurationManager.AppSettings
語法,甚至取不到數值會噴錯的問題
Super simple to use
Config API 提供了靜態方法 get(string key)
取出 <appSettings>
中的資料,web.config
與 app.config
皆通用這個方法
app.config
<appSettings>
<add key="Key" value="123" />
<add key="Name" value="Tom" />
</appSettings>
示範:
string key = Config.get("Key");
string name = Config.get("Name");
當取不到資料時,將回傳
null
Get Connection String
Config API 提供了直接取出在設定檔中的 Connection String 的靜態方法
app.config
<connectionStrings>
<add
name="NorthwindConnectionString"
connectionString="Data Source=serverName;Initial Catalog=Northwind;Persist Security Info=True;User ID=userName;Password=password"
providerName="System.Data.SqlClient" />
</connectionStrings>
示範:
string ConnStr = Config.getConnectionString("NorthwindConnectionString");
當取不到資料時,將回傳
null