.Net源码论坛 [ 繁体中文 ]

发新话题 回复该主题

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[0]), decimal.Parse(arr[1]));
                    }
                    return dic;
                }
            }
分享 转发
TOP
沙发

消灭0回复
TOP
板凳

消灭0回复
TOP
地板

1楼我又看见你了
TOP
五楼

回复 2楼wsvip的帖子

不明觉厉
TOP
六楼

回复 3楼czv3665336的帖子

不明觉厉
TOP
七楼

回复 4楼czv3665336的帖子

不明觉厉
TOP
八楼

谢谢了 嘿嘿
TOP
九楼

回复 8楼luoyaoyu的帖子

好吧
TOP
十楼

赚积分,小手一抖,积分到手
TOP
发新话题 回复该主题