通过Web.config中的configSections配置自己系统的全局常量
Posted 那就让我这样吧
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过Web.config中的configSections配置自己系统的全局常量相关的知识,希望对你有一定的参考价值。
通过Web.config中的configSections配置自己系统的全局常量
随着系统的庞大,你的全局信息保存在appsitting里可能会比较乱,不如为模块写个自定义的全局常量吧
首先在Web.Config文件中的代码可能是这样:
<configuration>
<configSections>
<section name="MyConfig" type="My.Core.Configuration.MyConfig, My.Core" requirePermission="false" />
</configSections>
<MyConfig>
<DynamicDiscovery Enabled="true" />
<Engine Type="" />
<Themes basePath="~/Themes/" />
<UserAgentStrings databasePath="~/App_Data/uas_20140512-01.ini" />
</MyConfig>
</configuration>
而YipongConfig可能是这样:
public object Create(object parent, object configContext, XmlNode section)
{
var config = new YipongConfig();
var dynamicDiscoveryNode = section.SelectSingleNode("DynamicDiscovery");
if (dynamicDiscoveryNode != null && dynamicDiscoveryNode.Attributes != null)
{
var attribute = dynamicDiscoveryNode.Attributes["Enabled"];
if (attribute != null)
config.DynamicDiscovery = Convert.ToBoolean(attribute.Value);
}
var engineNode = section.SelectSingleNode("Engine");
if (engineNode != null && engineNode.Attributes != null)
{
var attribute = engineNode.Attributes["Type"];
if (attribute != null)
config.EngineType = attribute.Value;
}
var startupNode = section.SelectSingleNode("Startup");
if (startupNode != null && startupNode.Attributes != null)
{
var attribute = startupNode.Attributes["IgnoreStartupTasks"];
if (attribute != null)
config.IgnoreStartupTasks = Convert.ToBoolean(attribute.Value);
}
var themeNode = section.SelectSingleNode("Themes");
if (themeNode != null && themeNode.Attributes != null)
{
var attribute = themeNode.Attributes["basePath"];
if (attribute != null)
config.ThemeBasePath = attribute.Value;
}
var userAgentStringsNode = section.SelectSingleNode("UserAgentStrings");
if (userAgentStringsNode != null && userAgentStringsNode.Attributes != null)
{
var attribute = userAgentStringsNode.Attributes["databasePath"];
if (attribute != null)
config.UserAgentStringsPath = attribute.Value;
}
return config;
}
/// <summary>
/// In addition to configured assemblies examine and load assemblies in the bin directory.
/// </summary>
public bool DynamicDiscovery { get; private set; }
/// <summary>
/// A custom <see cref="IEngine"/> to manage the application instead of the default.
/// </summary>
public string EngineType { get; private set; }
/// <summary>
/// Specifices where the themes will be stored (~/Themes/)
/// </summary>
public string ThemeBasePath { get; private set; }
/// <summary>
/// Indicates whether we should ignore startup tasks
/// </summary>
public bool IgnoreStartupTasks { get; private set; }
/// <summary>
/// Path to database with user agent strings
/// </summary>
public string UserAgentStringsPath { get; private set; }
}
调用就可以这样:
string myWebSiteName = ((NameValueCollection)ConfigurationSettings.GetConfig("SiteConfig"))["SiteName"];
以上是关于通过Web.config中的configSections配置自己系统的全局常量的主要内容,如果未能解决你的问题,请参考以下文章
为啥不能通过 IIS7 中的 web.config 删除“服务器”响应标头?
如何通过 web.config 文件中的 Access-Control-Allow-Origin 允许多个源域?
web.config配置文件中的configSource属性
如何通过web.config文件中的Access-Control-Allow-Origin允许多个源域?
git 中的 web.config 和 app.config 机器特定设置
是否可以通过 IIS 管理器中的用户界面在 web.config ASP .Net Web 应用程序中的 <system.Servicemodel> 中查看/编辑 WCF 配置?