1. Please firstly add reference System.Configuration
2.In web.config file you must register your own section under the path /configuration/configSections
Here I will show the example:
- <configuration>
- <configSections>
- <section name="appErrorSettings" type="System.Configuration.NameValueSectionHandler,System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
- </configSections>
- </configuration>
then register your own configuration using this section.
- <configuration>
- <appErrorSettings>
- <add key="File not found" value="Please check the existence of your file."/>
- <add key="Page not found" value="Hello user, The Page you requested is not found, please check your url."/>
- <add key="General Error" value="Support personnel have been notified of the error and will work to fix it shortly."/>
- <add key="Timeout Expired" value="Connection timed out. The network may be too busy. Please try after some time."/>
- <add key="Session Expired" value="Your session has expired. You will be re-directed to the Home page in 5 seconds."/>
- </appErrorSettings>
- </configuration>
3. Get the value using the following method
- public static string GetConfigValue(string section, string key)
- {
- System.Collections.Specialized.NameValueCollection nvc = (System.Collections.Specialized.NameValueCollection)System.Configuration.ConfigurationManager.GetSection(section);
- if (nvc != null)
- {
- if (nvc[key] != null)
- return nvc[key];
- else
- //Key does not exist
- return string.Empty;
- }
- else
- //Section does not exist
- return string.Empty;
- }