AppSettings 是很常放一些網站變數的地方 但每次 都要用 ConfigurationManager.AppSettings["ModuleTitle"]
去拿資料 打字起來就嫌長 所以 可以放到 Static class 這樣還可以有提示 跟 快取的效果 程式碼是參考文章內提供的
public class AppConfig
{
private static string _moduleTitle;
private static bool? _bundlingActive;
public static string ModuleTitle
{
get
{
if (string.IsNullOrEmpty(_moduleTitle))
_moduleTitle = ConfigurationManager.AppSettings["ModuleTitle"];
return _moduleTitle;
}
}
public static bool BundlingActive
{
get
{
if (!_bundlingActive.HasValue)
_bundlingActive = ConfigurationManager.AppSettings["BundlingActive"] == "1";
return _bundlingActive.Value;
}
}
}
Testing codes with ConfigurationManager.AppSettings
using System.Collections.Specialized;
using System.Configuration;
public interface IAppSettings
{
string this[string key] { get; }
}
public class AppSettingsWrapper : IAppSettings
{
private readonly NameValueCollection appSettings;
public AppSettingsWrapper()
{
this.appSettings = ConfigurationManager.AppSettings;
}
public string this[string key]
{
get
{
return this.appSettings[key];
}
}
}
如果內容有誤請多鞭策謝謝