zoukankan      html  css  js  c++  java
  • 通过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"];

  • 相关阅读:
    mybatis开始
    14-spring学习-变量操作
    java动态加载jar文件
    Linux下不借助工具实现远程linux服务器上传下载文件
    UML入门
    Linux下打开超大文件的方法
    List和Array相互转换
    redis常用命令
    redis不支持多个数据库实例但是支持多个字典
    如何配置 VirtualBox 中的客户机与宿主机之间的网络连接
  • 原文地址:https://www.cnblogs.com/niuzaihenmang/p/5619952.html
Copyright © 2011-2022 走看看