tags:

views:

37

answers:

2

Hey,

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

+3  A: 

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.)

kekekela
Thanks for the reply, That's correct the compilation debug is not what I am looking for. Can the value in the web.config be updated via the web browser, and how would you do that?
Scott
@Scott You can update the value using the AppSettings collection but it won't actually update the file, just what's in memory. To update the web.config you'd need to use standard xml file techniques, but it will reset your web app, which will clear out session, etc. If you need to update the actual file, its probably a better call to just move the settings into their own file that you can read from and update.
kekekela
What will cause the variable to be removed from memory, for example if I implement storing it in the web.config will it only effect the user's session and will it clear when I close my browser? This may be better than what I was looking for lol.
Scott
A: 

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.

klabranche
Thanks for the response, I haven't yet implemented a solution but this looks very promising, I won't forget to mark an answer when I am finished : )
Scott