Web.config use the configSource

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

In the asp.net if you modify the configuration files of web.config, will cause the application to restart, all answer (session) lost, in.NET Framework 2 and later, including all support   in a separate file; configSource  configuration element attribute configuration. This need not to restart the application, but also the management, avoid all the configuration on the web.config in a file so that the page looks messy. For example, appSetting, connectionStrings node.

Examples are as follows:

Note, the file path in configSouce can only for the relative physical path, it is only for the backslash (\), cannot use the slash(/).

The first is the web.config file:

<configuration>
    <!-- AppSettings website information configuration-->
    <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>

Here are two separate configuration file:

1,appSettings.config

 1 <?xml version="1.0" encoding="utf-8"?>
 2 
 3 <appSettings>
 4     <!-- Base parameter -->
 5     <add key="SiteResource" value="http://s.baidu.com"/>
 6     <add key="SiteUrl" value="http://www.baidu.com" />
 7     <add key="SiteName" value="www.baidu.com" />
 8     <add key="SiteKeyword" value="baidu"/>
 9     <add key="AllFreeShipping" value="false"/>
10     <add key="ReduceCashBegin" value="2013-9-10 16:00:00"/>
11     <add key="ReduceCashEnd" value="2013-9-16 16:00:00"/>
12     <add key="ReduceCashRule" value="500:30|400:25|300:20|200:15|100:10"/>
13 </appSettings>

2,connectionStrings.config

1 <?xml version="1.0"?>
2 <connectionStrings>
3     <add  name="connectionStrings"
4          connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
5          providerName="System.Data.SqlClient" />
6 </connectionStrings>

View Code


The same way reading time, just the same as before, here to write two:

 1   /// <summary>
 2         /// CSS,The JS reference.
 3         /// </summary>
 4         public static string SiteResource
 5         {
 6             get
 7             {
 8                 return ConfigurationManager.AppSettings["SiteResource"] as string;
 9             }
10         }
11         /// <summary>
12         /// By now the rules
13         /// </summary>
14         public static Dictionary<decimal, decimal> ReduceCashRule
15         {
16             get
17             {
18                 string val = ConfigurationManager.AppSettings["ReduceCashRule"] as string;
19                 string[] rule = val.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
20                 Dictionary<decimal, decimal> dic = new Dictionary<decimal, decimal>();
21                 foreach (string item in rule)
22                 {
23                     string[] arr = item.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
24                     dic.Add(decimal.Parse(arr[0]), decimal.Parse(arr[1]));
25                 }
26                 return dic;
27             }
28         }

In PS: on the nose, neat bangs look at faces, oblique bangs look temperament, no bangs look facial features... I am suited masked!!!!





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Kathie at November 15, 2013 - 5:21 AM