web.config里面使用configSource
在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的更多相关文章
- 在web.config里使用configSource分隔各类配置
转:http://www.yongfa365.com/Item/using-configSource-Split-Configs.html 大型项目中,可能有多个Service,也就是会有一堆配置,而 ...
- web.config中的configSource
在大型项目中,可能存在第三方类库的配置如:log4.net,AOP框架Unity,WCF等,或是自定义的配置,造成web.config内容过多,不易维护,影响Config初始化. 这时我们可以使用co ...
- Web.Config引入配置ConfigSource
1.配置文件要和Config文件通一个项目 2.注意路径的写法 3.appSettings和connectionStrings等都可以设置configSource 4.这样发布到不同的环境的时候,改动 ...
- 通过configSource提高web.config配置灵活性
很多时候我们会有这样的情况,开发环境和测试环境中的配置文件是不一样的,最明显的就是数据库连接串,这样,每次我们发布一个测试版本,都要手动去修改一下配置文件,是不是很麻烦的说.其实利用web.confi ...
- ASP.Net Web.config 中引用外部config文件
1. 前提准备: Web.config file: <?xml version="1.0" encoding="utf-8"?><config ...
- ASP.NET MVC系列:web.config中ConnectionString aspnet_iis加密与AppSettings独立文件
1. web.config中ConnectionString aspnet_iis加密 web.config路径:E:\Projects\Libing.Web\web.config <conne ...
- C# 灵活切换开发/测试/生成环境web.config
web.config <configuration> <connectionStrings configSource="config\Sit.db.config" ...
- Web.Config文件中使用configSource
我们都知道,在asp.net中修改了配置文件web.config后,会导致应用程序重启,所有会话(session)丢失.然而,应用程序的配置信息放在配置文件里是最佳选择,在后台修改了配置后导致所有会话 ...
- 在Web.Config文件中使用configSource,避免动态修改web.config导致asp.net重启(另添加一个Config文件用于管理用户数据)
原文:在Web.Config文件中使用configSource,避免动态修改web.config导致asp.net重启(另添加一个Config文件用于管理用户数据) 我们都知道,在asp.net中修改 ...
随机推荐
- Java内存区域分析
程序计数器 指令运行的指示器. 每一个线程都有独立的程序计数器,互无影响,我们称这类区域为线程私有的内存. 运行Java方法,计数器记录的是正在运行的虚拟机字节码指令地址;假设运行的是native方法 ...
- zTree实现基本树
zTree实现基本树 1.实现源代码 <!DOCTYPE html> <html> <head> <title>zTree实现基本树</title ...
- URL是什么?
URL是统一资源定位符,有时也被俗称为网页地址. 如同在网络上的门牌,是因特网上标准的资源的地址(Address). 在因特网的历史上,统一资源定位符的发明是一个非常基础的步骤.统一资源定位符的语法是 ...
- PrettyProgressBar
https://github.com/liuguangqiang/PrettyProgressBar
- ProgressBarLayoutView
https://github.com/alter-ego/ProgressBarLayoutView
- Windows平台下libevent库的使用
1 引子 手头上有一个使用了4个年头的HttpClient库,自己封装的,对于集成了IE浏览器的应用程序很友好.但最近想把产品扩展到Chrome和FireFox阵营,萌发了重构HttpClie ...
- 使用phonegap + appframework2.0框架
1.页面切换动画结束时卡(禁用动画) 2.搜索或导航标签需要固定(标签选择器动态修改高度) 3.pancel容器默认生成的时候内容不放 通过动态的的$("").empty().ht ...
- 剑指 offer set 9 包含min函数的栈
总结 1. 要求栈的 push, pop, min 都是 o(1). 普通栈支持 Push Pop 操作, 且时间复杂度已为 o(1), 再加上 Min 函数, 时间复杂度已无法优化, 只能通过加空间 ...
- SQL Server 查看死锁的存储过程(转载)
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_who_lock]') and ) drop proc ...
- C Primer
如果主函数使用void main(),由于它不是当前标准强制的一个选项,因此在有些系统上不能工作,为了使代码变得简单,main函数中可以没有返回语句. 先声明变量是为了防止拼写错误导致定义一个新的变量