Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC access application settings

Tags:

c#

asp.net-mvc

I am not sure how to use my application settings in my mvc4 application. For example I have an setting (stored in db), wether to use logging or not.

Should I load the setting once at application startup and then store it in the session-variable and check it against that? Or should I everytime i invoke an actionresult load the setting from database?

Whats the best practise here?

like image 756
Gmorken Avatar asked Feb 04 '26 17:02

Gmorken


2 Answers

You can keep this setting in web.config file:

 <appSettings>
        <add key="enableLogging" value="false"/>
 </appSettings>

And access it like this (requires reference to System.Configuration assembly):

ConfigurationManager.AppSettings["enableLogging"];

Note that this stores only string values, so you could parse it or just compare against string.

like image 153
Lars Avatar answered Feb 06 '26 06:02

Lars


in this case store it in :

HttpContext.Current.Cache 

storing it there will be available to all the requests and set the expire to 24 hours for example

this is not permanent storage , so whenever it expire you need to read it from DB

like image 35
sino Avatar answered Feb 06 '26 07:02

sino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!