传播公司可以做门户网站吗,网站建设的价钱,wordpress制作网站模板,建站平台wp近期一个项目需要写许多的配置项#xff0c;发现在单个web.config里面写的话会很乱也难于查找 所以搜了一下解决了#xff0c;记录下来 一、 webconfig提供了引入其他config的方式 connectionStrings configSourceConfigs\database.config / 这个是连接… 近期一个项目需要写许多的配置项发现在单个web.config里面写的话会很乱也难于查找 所以搜了一下解决了记录下来 一、 webconfig提供了引入其他config的方式 connectionStrings configSourceConfigs\database.config / 这个是连接字符串的配置你可以在database。config里面写很多链接字符串以备自己调用 database。config里面的内容如下 ?xml version1.0 encodingutf-8?
connectionStringsadd nameSDbContext connectionStringServer.;Initial CatalogSelf;User IDsa;Passwordpassword providerNameSystem.Data.SqlClient //connectionStrings appSettings configSourceConfigs\system.config / 这个是键值对的方式存放代码如下 ?xml version1.0 encodingutf-8?
appSettings!-- 1开发系统相关配置 --!-- 登陆提供者模式Session、Cookie--add keyLoginProvider valueCookie /!-- 启用系统日志--add keyIsLog valuetrue /!-- 数据库超时间--add keyCommandTimeout value180 /!--启用IP过滤 --add keyIsIPFilter valuefalse /!-- 2系统软件参数配置 --!-- 联系我们 --add keyContact valueTE Software(Mobility) /!-- 软件名称 --add keySoftName valueSub Self /!-- 软件版本 --add keyVersion value1.0 /!-- 设置就应用路径 --add keyAppName value /!-- 设置就应用路径 --add keySqlGetBomList value /
/appSettings 以上两个是不需要特殊的配置的放到configuration里面 configSections的下面这样就可以 二、下面介绍自定义配置节 configSectionssection nameusers typeSystem.Configuration.NameValueSectionHandler//configSectionsusers configSourceusers.config/users 注意configsections里面的一条是声明这是以什么组织方式 users.config 里面的内容如下 usersadd keybeijing value123/addadd keytianjin value123/add
/users 获取配置的方式 NameValueCollection users System.Configuration.ConfigurationManager.GetSection(users) as NameValueCollection;// Response.Write(users.Keys[0]/brusers.Keys[1]);
users.Get(beijing); 三、复杂类型 复杂类型的声明就不同了 configSectionssection nameroles typeEBuy.Chapter3.NTier.WebUI.RolesConfig, EBuy.Chapter3.NTier.WebUI//configSections
roles configSourceroles.config/roles 内容如下 rolesadd usernamebeijing password123/addadd usernametianjin password123/add
/roles 获取方式 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace EBuy.Chapter3.NTier.WebUI
{public class RolesConfig : System.Configuration.IConfigurationSectionHandler{public object Create(object parent, object configContext, System.Xml.XmlNode section){return section;}}
}XmlNode roles System.Configuration.ConfigurationManager.GetSection(roles) as XmlNode;Response.Write(roles.ChildNodes [0].Attributes[username].InnerText); 还可以配置为实体 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace EBuy.Chapter3.NTier.WebUI
{public class RolesConfig : System.Configuration.IConfigurationSectionHandler{public object Create(object parent, object configContext, System.Xml.XmlNode section){var listnew ListRole();for(int i0;isection.ChildNodes.Count;i){list.Add(new Role (){Username section.ChildNodes[i].Attributes[username].InnerText ,Password section.ChildNodes[i].Attributes[password].InnerText });}return list;}}public class Role{public string Username { get; set; }public string Password{get;set;}}
}var roles System.Configuration.ConfigurationManager.GetSection(roles) as ListEBuy.Chapter3.NTier.WebUI.Role ;Response.Write(roles.First ().Username);转载于:https://www.cnblogs.com/barnet/p/7132522.html