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;
}