views:

67

answers:

3

I may put the website on a different server and it has links linking it especially as it is on sharepoint so there are addresses and ports, so i thought it's better to save the link in Web.Config file in order not to need to change it a lot. So where is the best place or tag to put it in, like for example connection strings is at: configuration->connectionstrings.

+4  A: 

I have sometimes used appSettings to store link templates

<configuration>
  <appSettings>
    <add key="link.template1" value="http://example.com:1234/" />
  </appSettings>
</configuration>

Note that the url needs to be xml encoded, so if it includes querystring parameters you will need to encode the & characters to &amp; (and possibly encode other characters as well):

<configuration>
  <appSettings>
    <add key="link.template1" value="http://example.com/p1=value&amp;amp;p2=othervalue" />
  </appSettings>
</configuration>

...and of course I should have hinted on how to use the values. Thanks @Tchami for providing that answer in the comments; added here for answer completeness:

string urlTemplate = ConfigurationManager.AppSettings["link.template1"];

If you don't already have it, you will need to add a reference to System.Configuration to your project, and also a using System.Configuration statement in the beginning of the code file.

Fredrik Mörk
And how can I access it?applicationManagement.something.....?
Ahmad Farid
ConfigurationManager.AppSettings["key"]
Tchami
Thanks @Tchami. I sometimes wish that there was a mechanism for sharing rep points.
Fredrik Mörk
I tried to access it but it didn't read it although the connection strings are read. I even got the Count for the property but it's = ZERO!!
Ahmad Farid
@Ahmad: verify that you use the correct key, and that the web.config file is deployed properly (so that you are not using a previous version of it for instance).
Fredrik Mörk
WebConfigurationManager.AppSettings["link.template1] also works
Myra
this is the code that i have:<appSettings> <add key="k" value="v"/> </appSettings>andb.Text = ConfigurationManager.AppSettings.Count;the output is 0
Ahmad Farid
@Ahmad: and the <appSettings> element is a located directly in the <configuration> element? If I try this config file, I get `Count = 1`: `<configuration> <appSettings> <add key="k" value="v"/> </appSettings> </configuration>`
Fredrik Mörk
Thanks man. I am sorry it was a stupid mistake of me. Now it works pretty well. You rock ;)
Ahmad Farid
+1  A: 

Take a look at the AppSettings section.

Chris Pebble
A: 

I think you best bet here is to use a properties class that stores changes in the web.config. It's basically the same thing as using but it allows you to use either a default value set when you initially created the setting, or you can override it and use a value specified from the web.config file. If you're using VS2005 or VS2008, this is the way to go. Here's Microsoft's usage page on it: MSDN: Using Settings in C#

Relster