Scott
Scott

Reputation: 1003

Store & Return Settings for a Webpage

So I am using C# ASP.NET 3.5 and I would like to add a feature to my site to turn on and off a sort of debug mode for testing purposes.

Is there a best way to have a file or class that stores or returns simply if myDebug is on or off. It has to be accessed fast since it will be used a lot on multiple pages and it should be easy to set using the website itself.

My first thought is just a class with get/set which is stored on every page... perhaps the master page?

Thanks for any input -Scott

Upvotes: 1

Views: 116

Answers (2)

Kevin LaBranche
Kevin LaBranche

Reputation: 21078

Use AppSettings.

You can get your app settings like so:

string appSettingValue = ConfigurationManager.AppSettings["key"];

You can change your app settings like so.

Code below has been copied from blog link above to show as sample:

private void ChangeAppSettings(string key, string NewValue) 
{ 
Configuration cfg; 
cfg = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~"); 

KeyValueConfigurationElement setting = (KeyValueConfigurationElement)cfg.AppSettings.Settings(key); 

if ((setting != null)) { 
setting.Value = NewValue; 
cfg.Save(); 
} 
}

You might have to call ConfigurationManager.RefreshSection("appSettings"); after the save to have the app see the changes.... Im not sure if the cfg.Save() will reload/refresh the settings.

Upvotes: 0

heisenberg
heisenberg

Reputation: 9759

Sounds like something you'd want to put in AppSettings in your web.config.

(I'm assuming that setting compilation debug to true in web.config is insufficient for what you're trying to do.)

Upvotes: 3

Related Questions