技术开发 频道

cs配置文件的获取


【IT168技术文档】

  当我想看看如何获取SiteUrls.config的数据时,我就是找不到读取这个路径的url,且看SiteUrls的Instance方法

  path为载入的SiteUrls.config配置文件
string path = csContext.PhysicalPath(siteUrlProvider.Attributes["path"]); string overridePath = csContext.PhysicalPath(siteUrlProvider.Attributes["overridePath"]); Type type = Type.GetType(siteUrlProvider.Type); XmlDocument doc = null; if(File.Exists(overridePath)) { doc = Merger.MergeXmlFiles(path, overridePath); } else { doc = new XmlDocument(); doc.Load(path); } 于是我找到了Provider类 CSConfiguration config = CSConfiguration.GetConfig(); Provider siteUrlProvider = config.Providers["SiteUrlsDataProvider"] as Provider; 继续看CSConfiguration的Providers,其为一个哈希表,我们还不知道如何获取,CSConfiguration让我们看到了唯一的操作就是GetConfig public Hashtable Providers { get { return providers; } } 让我们再看GetConfig方法,下面的代码有点眉目了,我们终于看到了communityserver.config配置文件,好象跟我们想要的SiteUrls没关系,我们只要一个SiteUrls的路径而已. string path = null; string overridePath = null; XmlDocument doc = null; HttpContext context = HttpContext.Current; if (context != null) { path = context.Server.MapPath("~/communityserver.config"); overridePath = context.Server.MapPath("~/communityserver_override.config"); } else { path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "communityserver.config"); overridePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "communityserver_override.config"); } if(File.Exists(overridePath)) { doc = Merger.MergeXmlFiles(path, overridePath); } else { doc = new XmlDocument(); doc.Load(path); } 再看下来,开始真正实例化了 //实例化 config = new CSConfiguration(doc); 接着我们去看构造函数干了什么 public CSConfiguration(XmlDocument doc) { XmlDoc = doc; LoadValuesFromConfigurationXml(); } 再看LoadValuesFromConfigurationXml方法,看来真正做事情的是他 看到这里 foreach (XmlNode child in node.ChildNodes) { if (child.Name == "providers") GetProviders(child, providers); if (child.Name == "appLocation") GetAppLocation(child); if (child.Name == "extensionModules") GetProviders(child, extensions); if (child.Name == "roleConfiguration") GetRoleConfiguration(child); if (child.Name == "filterLanguages") GetFilterLanguages(child); if (child.Name == "editors") GetEditors(child); } 打开配置文件看看发现以下节点 <add name = "SiteUrlsDataProvider" type = "CommunityServer.Urls.UrlsData, CommunityServer.Urls" path = "SiteUrls.config" overridePath ="SiteUrls_override.config" /> internal void GetProviders(XmlNode node, Hashtable table) { foreach (XmlNode provider in node.ChildNodes) { switch (provider.Name) { case "add" : table.Add(provider.Attributes["name"].Value, new Provider(provider.Attributes) ); break; case "remove" : table.Remove(provider.Attributes["name"].Value); break; case "clear" : table.Clear(); break; } } }
0
相关文章