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中修改 ...
随机推荐
- Flex圆角矩形
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="h ...
- web及移动应用测试知识总结
发现自己对测试知识的掌握不够系统,在这里整理一下好了. 1. 通用测试点 功能测试 正向:输入一个有效的输入并且期望软件能够完成一些根据说明书规定的行为 逆向:输入一个无效的输入并且期望软件给出合理的 ...
- jquery 预览提交的表单
预览表单,查看后确认提交或者返回重填 演示 XML/HTML Code <form class="mform" id="myform" method=&q ...
- defer属性---->执行外部脚本
HTML4---->只有IE支持 不需要是外部脚本. HTML5---->主流都支持 defer 属性仅适用于外部脚本(只有在使用 src 属性时) 值 描述 defer 规定当页面已完成 ...
- 写一个自己定义进度颜色和圆形转动的ProgressBar(具体介绍)
先上图: 我们得自己定义ProgressBar的样式 <span style="white-space:pre"> </span><style nam ...
- jquery.validate+jquery.form提交的三种方式
原文:http://www.cnblogs.com/datoubaba/archive/2012/06/06/2538873.html jquery.validate+jquery.form提交的三种 ...
- ASP.NET项目从VS2008迁移至VS2010或2012编译不过的问题
这次将一个VS2008编写的网站迁移至VS2010或者2012下都编译不通过,统统报相同的错误:缺少System.Linq引用,缺少System.Xml.Linq引用,但是明明有引用啊,引用DLL我都 ...
- springMVC与struts2的区别
1. 机制:spring mvc的入口是servlet,而struts2是filter,这样就导致了二者的机制不同. 2. 性能:spring会稍微比struts快.spring mvc是基于方法的设 ...
- Working with MTD Devices
转:http://www.linuxforu.com/2012/01/working-with-mtd-devices/ Working with MTD Devices By Mohan Lal J ...
- 【技术文档】XuebaOnline配环境时遇到的问题和解决办法
在Ubuntu下装XuebaOnline可能遇到的问题和解决办法 自动安装Python3.0以上版本 编译命令采用python3 manage.py runserver,所以在linux系统下需要安装 ...