Custom values in Web.config

T

Tumurbaatar S.

Hi!
In old ASP I used Application collection to store configuration settings
like ADO connection string. In .NET, it seems, the preferred method is
using Web.config file. Yes? If I'm right then how to add (manually) and
retrieve (from app) custom values?

Thank you!

P.S. It seems, VS.NET does not have tool to modify Web.config file.
Developer should update this file manually as text?
 
L

Lars-Erik Aabech

Hi!

Yes, web.config is the file to use. You can also use machine.config for
stuff like connection strings so they won't be stored on a public area. The
config files are XML files, so you can use any XML editor to edit them. ;)

In your web.config file, add an element called <appSettings> between
<configuration> and <system.web>, inside the appSettings element, use the
<add/> element to add variables:

<configuration>
<appSettings>
<add key="SomeKey" value="SomeValue"/>
....
</appSettings>
<system.web>
.....
</configuration>

Now you can access these values by the
System.Configuration.ConfigurationSettings class' static AppSettings
property. Like this:

using System.Configuration;
....
public class MyPage : System.Web.Page
{
....
public void Page_OnLoad()
{
Response.Write(ConfigurationSettings.AppSettings.Get("SomeKey"));
}
....
}


By the way.. Windows Applications can also use config files. Name the file
yourprogram.exe.config and you're up and running. ;)

HTH,
Lars-Erik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top