在asp.net中如果修改了配置文件web.config以后,会导致应用程序重启,所有回话(session)丢失掉,在 .NET Framework 2.0 以后的版本中,可以在一个单独文件中包括所有支持 class="keyword">configSource 属性的配置元素的配置。这样既不用重启应用程序,也方面管理,避免把所有的配置都放在web.config一个文件里使页面看起来比较乱。例如appSetting、connectionStrings节点。
例子如下:
注意,configSouce中的文件路径只能为相对物理路径,也就是只能为反斜杠(\),不能用斜杠(/)。
首先是web.config文件:
logs_code_hide('82e3b377-5606-45d4-8c65-b35ad57f79e0',event)" src="/Upload/Images/2013091720/2B1B950FA3DF188F.gif" alt="" /><configuration> <!-- appSettings网站信息配置--> <appSettings configSource="config\appSettings.config" /> <connectionStrings configSource="config\connectionStrings.config"/> <system.web> <compilation debug="true" targetFramework="4.0"/> <httpHandlers configSource="config\httpHandlers.config" /> <authentication mode="Forms"> <forms loginUrl="~/Account/Login.aspx" timeout="2880" /> </authentication> <pages configSource="config\pages.config" /> <membership> <providers> <clear/> <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" /> </providers> </membership> <profile> <providers> <clear/> <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/> </providers> </profile> <roleManager enabled="false"> <providers> <clear/> <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" /> <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" /> </providers> </roleManager> </system.web> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration>View Code
下面是两个单独的配置文件:
1、appSettings.config
1 <?xml version="1.0" encoding="utf-8"?> 2 3 <appSettings> 4 <!-- Base parameter --> 5 <add key="SiteResource" value="http://s.baidu.com"/> 6 <add key="SiteUrl" value="http://www.baidu.com" /> 7 <add key="SiteName" value="www.baidu.com" /> 8 <add key="SiteKeyword" value="baidu"/> 9 <add key="AllFreeShipping" value="false"/> 10 <add key="ReduceCashBegin" value="2013-9-10 16:00:00"/> 11 <add key="ReduceCashEnd" value="2013-9-16 16:00:00"/> 12 <add key="ReduceCashRule" value="500:30|400:25|300:20|200:15|100:10"/> 13 </appSettings>View Code
2、connectionStrings.config
1 <?xml version="1.0"?> 2 <connectionStrings> 3 <add name="connectionStrings" 4 connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" 5 providerName="System.Data.SqlClient" /> 6 </connectionStrings>View Code
读取的时候的方式不变,跟以前一样,这里就写两个:
1 /// <summary> 2 /// CSS、JS引用地址 3 /// </summary> 4 public static string SiteResource 5 { 6 get 7 { 8 return ConfigurationManager.AppSettings["SiteResource"] as string; 9 } 10 } 11 /// <summary> 12 /// 减现规则 13 /// </summary> 14 public static Dictionary<decimal, decimal> ReduceCashRule 15 { 16 get 17 { 18 string val = ConfigurationManager.AppSettings["ReduceCashRule"] as string; 19 string[] rule = val.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); 20 Dictionary<decimal, decimal> dic = new Dictionary<decimal, decimal>(); 21 foreach (string item in rule) 22 { 23 string[] arr = item.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries); 24 dic.Add(decimal.Parse(arr[0]), decimal.Parse(arr[1])); 25 } 26 return dic; 27 } 28 }View Code
PS:中分看鼻子,齐刘海看脸型,斜刘海看气质,无刘海看五官。。。我适合蒙面!!!!