通过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配置自己系统的全局常量的更多相关文章

  1. [转]通过继承ConfigurationSection,在web.config中增加自定义配置

    本文转自:http://www.blue1000.com/bkhtml/2008-02/55810.htm 前几天写了一篇使用IConfigurationSectionHandler在web.conf ...

  2. 释放SQL Server占用的内存 .Net 读取xml UrlReWriter 在web.config中简单的配置

    释放SQL Server占用的内存   由于Sql Server对于系统内存的管理策略是有多少占多少,除非系统内存不够用了(大约到剩余内存为4M左右),Sql Server才会释放一点点内存.所以很多 ...

  3. 加密web.config中的邮件配置mailSettings

    加密: 在命令提示符下键入: aspnet_regiis -pef connectionStrings 要加密的web.config完整路经 演示样例:C:\Program Files (x86)\M ...

  4. 使用IConfigurationSectionHandler在web.config中增加自定义配置

    一. 场景    这里仅举一个简单应用的例子,我希望在web.config里面增加网站的基本信息,如:网站名称,网站版本号,是否将网站暂时关闭等.二. 基本实现方法1. 定义配置节点对应的类:Site ...

  5. ASP.NET,web.config 中SessionState的配置

    web Form 网页是基于HTTP的,它们没有状态, 这意味着它们不知道所有的请求是否来自同一台客户端计算机,网页是受到了破坏,以及是否得到了刷新,这样就可能造成信息的丢失. 于是, 状态管理就成了 ...

  6. web.config中namespace的配置(针对页面中引用)

    1,在页面中使用强类型时: @model GZUAboutModel @using Nop.Admin.Models//命名空间(注意以下) 2,可以将命名空间提到web.config配置文件中去,此 ...

  7. .Net高级编程-自定义错误页 web.config中<customErrors>节点配置

    错误页 1.当页面发生错误的时候,ASP.Net会将错误信息展示出来(Sqlconnection的错误就能暴露连接字符串),这样一来不好看,二来泄露网站的内部实现信息,给网站带来安全隐患,因此需要定制 ...

  8. [转]WinForm和WebForm下读取app.config web.config 中邮件配置的方法

    本文转自:http://blog.csdn.net/jinbinhan/article/details/1598386 1. 在WinForm下读取 App.config中的邮件配置语句如下: Con ...

  9. web.config中authorization下的location中的path的设置 (转)

    项目下 有三个文件夹 A,B,C 验正方式是 Forms 验正 我要设置他们的访问权限为, A,匿名可访问 B,普通用户授权后才能访问 C,只允许管理员访问 <configuration> ...

随机推荐

  1. linux cross toolsChain 交叉编译 ARM(转)

    转载请注明出处:http://blog.csdn.net/mybelief321/article/details/9076583 安装环境 Linux版本:Ubuntu 12.04    内核版本:L ...

  2. net 内存泄露和内存溢出

    一直以来都对内存泄露和内存溢出理解的不是很深刻.在网上看到了几篇文章,于是整理了一下自己对内存泄露和内存溢出的理解. 一.概念 内存溢出:指程序在运行的过程中,程序对内存的需求超过了超过了计算机分配给 ...

  3. 关于mysql的error-based injection payload

    今天用sqlmap扫一个网站,想得到一个表的所有列名,注意到sqlmap用到的一个error-based payload的: 1 AND (SELECT 3174 FROM(SELECT COUNT( ...

  4. java 小结2 多态问题和容器介绍

    面向对象这个东西,其实我们一直是不是都没有感觉到自己在用,以后我一定要用用.以前学c#时候认真的看过一次,最近一直研究java.随便再看看. 多态问题: 在java中多态分为(1)编译时多态和(2)运 ...

  5. UVa1424–Salesmen(DP)

    题目大意 给定一个包含n(n<=100)个点的无向连通图和一个长度为L的序列A(L<=200),你的任务是修改尽量少的数,使得序列中的任意两个相邻的数或者相同,或者对应图中两个相邻结点 题 ...

  6. Java 下 SSL 通信原理及实例

    有关SSL的原理和介绍在网上已经有不少,对于Java下使用keytool生成证书,配置SSL通信的教程也非常多.但如果我们不能够亲自动手做一个SSL Sever和SSL Client,可能就永远也不能 ...

  7. 【转】Vim 常用命令总结

    使用 Vim 的时间不长,但如今已经离不开熟悉的 Vim 编辑模式了. Vim 的学习曲线是非常陡的,一开始学习的时候,面对很多的操作命令要去记住,常常望而却步. 其实,只要记住一些常用的命令,加之在 ...

  8. Git学习资料

    1.http://wuyuans.com/2012/05/github-simple-tutorial/ 2.http://www.ihref.com/read-16369.html

  9. MySQL 5.7 for Windows 解压缩版 MySQL 服务无法启动

    MySQL 5.7 for Windows 解压缩版配置安装 http://jingyan.baidu.com/article/f3ad7d0ffc061a09c3345bf0.html basedi ...

  10. 版本控制Subversion TortoiseSVN apache VisualSVN笔记(转载)

    转载于http://blog.sina.com.cn/s/blog_6b94d5680101m7ah.html Subversion(简称svn)是近年来崛起的版本管理软件,是cvs的接班人.目前,绝 ...