全局可访问的mvc Web应用程序配置?

时间:2013-10-09 07:54:40

标签: c# asp.net-mvc asp.net-mvc-4 configuration

一旦我的Web应用程序启动,它就会从自定义配置文件(!not web.config )中加载一些数据库连接参数,创建一个数据库实例并需要获取大约120个不同的数据库中正确工作所需的参数。我现在的问题是 - 让它们全球可访问的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

您可以使用每个参数的属性或包含所有参数的字典在其周围创建静态包装:

public static class ConfigParams
{
    // Either properties for every parameter:

    public static string Culture { get; set; }

    public static string WebsiteTitle { get; set; }

    public static DateTime LastUpdate { get; set; }

    // Or a dictionary containing all the properties accessed by a string:

    Dictionairy<string, object> Params { get; set; }    
}

然后像这样访问它们:

string culture = ConfigParams.Culture;

或者使用字典:

string culture = ConfigParams.Params["Culture"].ToString();