将多个配置文件用于共享MVC项目

时间:2011-10-13 15:15:06

标签: asp.net asp.net-mvc configuration

我有一个MVC应用程序,它包含一个目录,其中包含除css和master视图之外的所有必需文件。在IIS中,我设置了几个使用相同目录的站点,但随后为每个站点的唯一css和模板设置了虚拟目录。这非常有效,虽然我现在想要为每个唯一的站点单独配置(对于基本设置,如站点ID,以查询我的数据库等)。我为每个站点创建了新的虚拟目录,并在每个站点中添加了一个config.xml,在应用程序启动方法中将以下内容添加到我的global.asax.cs文件中:

protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);

        //read in all the application variables from the config file on startup
        XmlDocument Doc = new XmlDocument();
        Doc.Load(Server.MapPath("/Config/Config.xml"));

        XmlNodeList VarLst = Doc.DocumentElement.SelectNodes("//AppVars/*");
        foreach (XmlNode VarNod in VarLst)
        {
            RecursiveDescent(VarNod, "");
        }
        string[] AppVars = new string[Application.Count];
        AppVars = Application.AllKeys;

        //How do I now use Application.AllKeys from a controller?
    }

现在,我有我的钥匙,但似乎无法弄清楚如何在控制器中使用它们。通常情况下,如果我在网络配置中使用应用程序设置并想设置我的siteId,我只会使用:

int siteId = Convert.ToInt16(WebConfigurationManager.AppSettings["SiteId"]);

但现在我想用

Application["SiteId"];

但它不像是从页面类继承的webforms代码,我没有得到关于如何在MVC控制器中获取应用程序设置的概念。

1 个答案:

答案 0 :(得分:1)

根据另一个问题:Access "Application" object in ASP.Net MVC to store application wide variables

this.HttpContext.Application["SiteId"]