在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="http://s.baidu.com"/>
<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[]), decimal.Parse(arr[]));
}
return dic;
}
}

PS:中分看鼻子,齐刘海看脸型,斜刘海看气质,无刘海看五官。。。我适合蒙面!!!!

web.config里面使用configSource的更多相关文章

  1. 在web.config里使用configSource分隔各类配置

    转:http://www.yongfa365.com/Item/using-configSource-Split-Configs.html 大型项目中,可能有多个Service,也就是会有一堆配置,而 ...

  2. web.config中的configSource

    在大型项目中,可能存在第三方类库的配置如:log4.net,AOP框架Unity,WCF等,或是自定义的配置,造成web.config内容过多,不易维护,影响Config初始化. 这时我们可以使用co ...

  3. Web.Config引入配置ConfigSource

    1.配置文件要和Config文件通一个项目 2.注意路径的写法 3.appSettings和connectionStrings等都可以设置configSource 4.这样发布到不同的环境的时候,改动 ...

  4. 通过configSource提高web.config配置灵活性

    很多时候我们会有这样的情况,开发环境和测试环境中的配置文件是不一样的,最明显的就是数据库连接串,这样,每次我们发布一个测试版本,都要手动去修改一下配置文件,是不是很麻烦的说.其实利用web.confi ...

  5. ASP.Net Web.config 中引用外部config文件

    1. 前提准备: Web.config file: <?xml version="1.0" encoding="utf-8"?><config ...

  6. ASP.NET MVC系列:web.config中ConnectionString aspnet_iis加密与AppSettings独立文件

    1. web.config中ConnectionString aspnet_iis加密 web.config路径:E:\Projects\Libing.Web\web.config <conne ...

  7. C# 灵活切换开发/测试/生成环境web.config

    web.config <configuration> <connectionStrings configSource="config\Sit.db.config" ...

  8. Web.Config文件中使用configSource

    我们都知道,在asp.net中修改了配置文件web.config后,会导致应用程序重启,所有会话(session)丢失.然而,应用程序的配置信息放在配置文件里是最佳选择,在后台修改了配置后导致所有会话 ...

  9. 在Web.Config文件中使用configSource,避免动态修改web.config导致asp.net重启(另添加一个Config文件用于管理用户数据)

    原文:在Web.Config文件中使用configSource,避免动态修改web.config导致asp.net重启(另添加一个Config文件用于管理用户数据) 我们都知道,在asp.net中修改 ...

随机推荐

  1. C语言综述

    1.预处理指令:在变异之前执行的指令. 系统自带的文件用<>,自己写的文件用""; .h成为头文件,用来声明一些常用的函数,假如想使用这些函数,就必须包含这个头文件(注 ...

  2. tomcat如何简单调优

    我们在javaEE开发的过程中,经常会进行tomcat调优操作,下面我们来简单讲解一下tomcat调优. 1) 去掉web.xml的监视,提前将jsp编译成servlet. 2)在物理内存允许的范围内 ...

  3. registerClassAlias()函数和getClassByAlias()函数

    flash.net 包中包含包级函数,可用于打开新的浏览器窗口,向服务器发送 URL 请求以及处理类别名. registerClassAlias()函数 public function registe ...

  4. 解决黑苹果与windows时区不一致

    原理就是将windows识别硬件时间为UTC-0而不是现在的UTC+8 下面都是抄来的 注册表HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\T ...

  5. TP复习17

    三大自动,自动创建,自动验证,自动完成

  6. zoj-3626 Treasure Hunt I (树形dp)

    本文出自   http://blog.csdn.net/shuangde800 题目链接: zoj-3626 题意 给一棵n个节点的树, 节点编号1~n, 每个节点有权值val[i],经过这个节点就可 ...

  7. [ES6] 15. Generators -- 2

    Using for..of statement: function* greeting(){ console.log(`Generators are "lazy"`); yield ...

  8. Java final修饰形参

    转自:http://java.chinaitlab.com/base/836044.html public class BB{ public int i; } public class PP{ pub ...

  9. 创建透明的UIToolbar

    TranslucentToolbar.h文件 #import <UIKit/UIKit.h> @interface TranslucentToolbar : UIToolbar @end ...

  10. cisco路由基于策略的路由选择

    cisco路由基于策略的路由选择 基于策略的路由选择是一种手段,通过它管理员可以在基于目的地的路由选择协议中实现偏离标准路由的路由选择.基于目的地的路由选择协议将根据到一个目的地的最短路径选择路由,基 ...