简体   繁体   English

MVC访问应用程序设置

[英]MVC access application settings

I am not sure how to use my application settings in my mvc4 application. 我不知道如何在我的mvc4应用程序中使用我的应用程序设置。 For example I have an setting (stored in db), wether to use logging or not. 例如,我有一个设置(存储在db中),是否使用日志记录。

Should I load the setting once at application startup and then store it in the session-variable and check it against that? 我应该在应用程序启动时加载设置一次,然后将其存储在session-variable中并对其进行检查? Or should I everytime i invoke an actionresult load the setting from database? 或者我应该每次调用一个actionresult从数据库加载设置?

Whats the best practise here? 什么是最好的做法?

You can keep this setting in web.config file: 您可以在web.config文件中保留此设置:

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

And access it like this (requires reference to System.Configuration assembly): 并像这样访问它(需要引用System.Configuration程序集):

ConfigurationManager.AppSettings["enableLogging"];

Note that this stores only string values, so you could parse it or just compare against string. 请注意,这仅存储字符串值,因此您可以解析它或仅与字符串进行比较。

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 存储它将可用于所有请求,并将过期时间设置为24小时

this is not permanent storage , so whenever it expire you need to read it from DB 这不是永久存储,因此无论何时到期,您都需要从DB中读取它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM