framework 2.0 read appsettings from web.config

0

I am working on an application that is compiled with framnework 2.0 for compatibility reasons, you can not pass the app to another framework. I need to read a value from the web.config file from the appsettings section.

Does anyone know any way to do this with this particular framework?

Greetings

    
asked by Luis Gabriel Fabres 16.08.2018 в 17:11
source

2 answers

1

In your config file you declare the following within the node

<appSettings>
    <add key="MyKey" value="true" />
</appSettings>

In your code you get it

string my_key = System.ConfigurationManager.AppSettings["MyKey"]

You may have to add the System.Configuration reference to your project because it does not recognize the ConfigurationManager class on its own.

    
answered by 16.08.2018 в 17:18
1

You have several options according to the "needs"

Of course you have the form by code, there we are helped by the class ConfigurationManager inside the namespaces System.configuration

Example if we have

<appSettings>
   <add key="ForceSide" value="DarkSide"/>
 </appSettings>

To get its value

ForceSide.Text = ConfigurationManager.AppSettings["ForceSide"];

For the full name with namespaces

ForceSide.Text = System.Configuration.ConfigurationManager.AppSettings["ForceSide"];

You can also read it directly on webcontrols with what is called ASP.NET Expressions Example in a literal ...

<asp:Literal ID="ForceSide2" Text="<%$ AppSettings: ForceSide %>" runat="server" />

NOTE: Remember that it starts with the $ sign (which we usually place =) ..Text=" <% $ AppSettings:

Links that can help you:

answered by 16.08.2018 в 18:08