原文地址:http://www.cnblogs.com/huangxiufen/archive/2012/09/07/2668584.html
因为公司想要通过静动态分离的方式使得网站访问速度更快速,所以选择了分布式加载网站中的静态文件(js,css,images),当然还有另外一些方法,这边就不做深入探讨(因为我也不是很清楚有多少方法),下面介绍下通过代码方式读取web.config的配置内容,从而实现分布加载静态文件。
首先,在web.config写我们需要的配置内容:
<gitom> <!--分布式节点--> <distribution>
<!--文件版本--> <fileVersion version="20120811_test"/>
<!--enabled:表示分布式是不是可用 -->
<!--defaultServerName:默认服务器的名称 -->
<!--random:随机,是否要以随机获取服务器的方式 --> <staticServers enabled="false" defaultServerName="Cdn1" random="true"> <staticServer name="Cdn1" url="http://js.uc.gitom.cn"/> </staticServers> <!--<uploadServers enabled="true"> <uploadServer name="Cdn1" url="http://js.uc.gitom.cn"/> </uploadServers>--> </distribution> </gitom>
当然,还要做的一件事情就是声明节点,因为这些节点是我们自定义的。
第二,写一个读取的类DistributionSectionHandle.cs,写成类的方式,实现可复用。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Configuration; 6 using System.Collections; 7 8 namespace Gitom.Configuration 9 { 10 /// <summary> 11 /// 分布式配置节 12 /// </summary> 13 public class DistributionSectionHandler : ConfigurationSection 14 { 15 public static DistributionSectionHandler DistributionConfig 16 { 17 get 18 { 19 return ConfigurationManager.GetSection("gitom/distribution") as DistributionSectionHandler; 20 } 21 } 22 23 /// <summary> 24 /// 分布式服务器集合 25 /// </summary> 26 [ConfigurationProperty("staticServers", IsDefaultCollection = false)] 27 public StaticServerConfigurationElementCollection StaticServers 28 { 29 get 30 { 31 return (StaticServerConfigurationElementCollection)base["staticServers"]; 32 } 33 } 34 35 /// <summary> 36 /// 文件版本配置节(可选) 37 /// </summary> 38 [ConfigurationProperty("fileVersion", IsRequired = false)] 39 public FileVersionConfigurationElement FileVersion 40 { 41 get { return (FileVersionConfigurationElement)base["fileVersion"]; } 42 } 43 } 44 45 /// <summary> 46 /// 文件版本配置节 47 /// </summary> 48 public class FileVersionConfigurationElement : ConfigurationElement 49 { 50 [ConfigurationProperty("version", IsRequired = true)] 51 public string Version 52 { 53 get { return (string)base["version"]; } 54 } 55 56 57 } 58 59 /// <summary> 60 /// 分布式服务器配置节 61 /// </summary> 62 public class StaticServerConfigurationElementCollection : ConfigurationElementCollection 63 { 64 65 /// <summary> 66 /// 用于缓存数据 67 /// </summary> 68 private Hashtable htServers = new Hashtable(); 69 70 /// <summary> 71 /// 是否为缓存加载,true:是,false:否 72 /// </summary> 73 private bool cacheLoaded = false; 74 75 /// <summary> 76 /// 是否启用 77 /// </summary> 78 [ConfigurationProperty("enabled", IsRequired = true)] 79 public bool Enabled 80 { 81 get { return (bool)base["enabled"]; } 82 } 83 84 /// <summary> 85 /// 是否随机选择服务器 86 /// </summary> 87 [ConfigurationProperty("random", IsRequired = false, DefaultValue = true)] 88 public bool Random 89 { 90 get { return (bool)base["random"]; } 91 } 92 93 /// <summary> 94 /// 默认服务器名,即没有随机的情况下,选择哪个名称 95 /// </summary> 96 [ConfigurationProperty("defaultServerName", IsRequired = false, DefaultValue = null)] 97 public string DefaultServerName 98 { 99 get { return (string)base["defaultServerName"]; } 100 } 101 102 public override ConfigurationElementCollectionType CollectionType 103 { 104 get 105 { 106 return ConfigurationElementCollectionType.BasicMap; 107 } 108 } 109 110 public new StaticServerConfigurationElement this[string name] 111 { 112 get 113 { 114 foreach (ConfigurationElement ce in this) 115 { 116 if ((ce as StaticServerConfigurationElement).Name == name) 117 { 118 return ce as StaticServerConfigurationElement; 119 } 120 } 121 return null; 122 } 123 } 124 125 public StaticServerConfigurationElement this[int index] 126 { 127 128 get 129 { 130 131 int tmp = 0; 132 133 foreach (StaticServerConfigurationElement serverElement in this) 134 { 135 // 第一次加载,缓存里没数据 136 if (!cacheLoaded) 137 { 138 htServers.Add(tmp, serverElement); 139 tmp++; 140 } 141 142 } 143 144 // 设置缓存加载为true 145 cacheLoaded = true; 146 147 if (htServers.Count - 1 < index) 148 { 149 return null; 150 } 151 152 return (StaticServerConfigurationElement)htServers[index]; 153 } 154 } 155 156 protected override string ElementName 157 { 158 get 159 { 160 return "staticServer"; 161 } 162 } 163 164 protected override ConfigurationElement CreateNewElement() 165 { 166 return new StaticServerConfigurationElement(); 167 } 168 169 protected override object GetElementKey(ConfigurationElement element) 170 { 171 return ((StaticServerConfigurationElement)element).Name; 172 } 173 } 174 175 public class StaticServerConfigurationElement : ConfigurationElement 176 { 177 [ConfigurationProperty("name", IsRequired = true)] 178 public string Name 179 { 180 get 181 { 182 return (string)base["name"]; 183 } 184 } 185 186 [ConfigurationProperty("url", IsRequired = true)] 187 public string Url 188 { 189 get 190 { 191 return (string)base["url"]; 192 } 193 } 194 } 195 }