MVC頁面擴展方法 單例模式 /// <summary> /// 創建一個Config內容對象 /// </summary> /// <param name=""></param> /// <param name="key"></param> /// <returns></returns> publ ...
MVC頁面擴展方法 單例模式 /// <summary> /// 創建一個Config內容對象 /// </summary> /// <param name=""></param> /// <param name="key"></param> /// <returns></returns> public static MvcHtmlString ConfigSetting(this HtmlHelper helper, string key) { var str = ConfigHelper.GetValue(key); return MvcHtmlString.Create(str); } 調用:@Html.ConfigSetting("contact-company-message") public static class ConfigHelper { static Dictionary<string,Configer> dic = null; static void InitClass() { if(dic == null) { using(var db = new Entities()) { dic = db.Configer.ToDictionary(r=>r.Key) } } } } publicsh static void Refresh() { usring(var db = new Entitiey()) { dic = db.configer.ToDictory(r=>r.key); } } publist static string GetValut(string key) { InitClass(); if(!dic.keys.Contains(key)) returnll; return dic[key].value; }