转载 :http://www.cnblogs.com/jiaxa/p/3326631.html?utm_source=tuicool
在asp.net中如果修改了配置文件web.config以后,会导致应用程序重启,所有回话(session)丢失掉,在 .NET Framework 2.0 以后的版本中,可以在一个单独文件中包括所有支持 configSource 属性的配置元素的配置。这样既不用重启应用程序,也方面管理,避免把所有的配置都放在web.config一个文件里使页面看起来比较乱。例如appSetting、connectionStrings节点。
例子如下:
注意,configSouce中的文件路径只能为相对物理路径,也就是只能为反斜杠(\),不能用斜杠(/)。
首先是web.config文件:
<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>
下面是两个单独的配置文件:
1、appSettings.config
<?xml version="1.0" encoding="utf-8"?> <appSettings> <!-- Base parameter --> <add key="SiteResource" value=" <add key="SiteUrl" value="http://www.baidu.com" /> <add key="SiteName" value="www.baidu.com" /> <add key="SiteKeyword" value="baidu"/> <add key="AllFreeShipping" value="false"/> <add key="ReduceCashBegin" value="2013-9-10 16:00:00"/> <add key="ReduceCashEnd" value="2013-9-16 16:00:00"/> <add key="ReduceCashRule" value="500:30|400:25|300:20|200:15|100:10"/> </appSettings>
2、connectionStrings.config
<?xml version="1.0"?> <connectionStrings> <add name="connectionStrings" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" /> </connectionStrings>
读取的时候的方式不变,跟以前一样,这里就写两个:
/// <summary>
/// CSS、JS引用地址
/// </summary>
public static string SiteResource
{
get
{
return ConfigurationManager.AppSettings["SiteResource"] as string;
}
}
/// <summary>
/// 减现规则
/// </summary>
public static Dictionary<decimal, decimal> ReduceCashRule
{
get
{
string val = ConfigurationManager.AppSettings["ReduceCashRule"] as string;
string[] rule = val.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
Dictionary<decimal, decimal> dic = new Dictionary<decimal, decimal>();
foreach (string item in rule)
{
string[] arr = item.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
dic.Add(decimal.Parse(arr[0]), decimal.Parse(arr[1]));
}
return dic;
}
}
转载于:https://blog.51cto.com/254240/1652692