[英]MVC 5 Application Settings
所以我想在应用程序中存储一些设置,这些设置我只能从中读取(我认为这很不错)。
我如何注入阅读器,我不完全确定如何首先阅读应用程序设置,或者它是否已经具有注入接口。
我要么想要这样的事情:
public interface IPropertyService
{
string ReadProperty(string key);
}
然后实施:
public class DefaultPropertyService : IPropertyService
{
public string ReadProperty(string key)
{
// what ever code that needs to go here
// to call the application settings reader etc.
return ApplicationSetting[key];
}
}
在这个问题上的任何帮助都会很棒
您要在哪里存储应用程序设置? Web.Config的appSettings部分不足吗?
<appSettings>
<add key="someSetting" value="SomeValue"/>
</appSettings>
然后以这种方式阅读您的设置
ConfigurationManager.AppSettings["someSetting"]
您有正确的想法,并且基本上就在那里。 对于Web,配置设置存储在AppSettings
节点下的Web.Config
。 您可以使用ConfigurationManager.AppSettings
以代码访问它们。 这是访问配置的可注入服务的示例实现。
public interface IPropertyService {
string ReadProperty(string key);
bool HasProperty(string key);
} // end interface IPropertyService
public class WebConfigurationPropertyService : IPropertyService {
public WebConfigurationPropertyService() {
} // end constructor
public virtual bool HasProperty(string key) {
return !String.IsNullOrWhiteSpace(key) && ConfigurationManager.AppSettings.AllKeys.Select((string x) => x).Contains(key);
} // end method HasProperty
public virtual string ReadProperty(string key) {
string returnValue = String.Empty;
if(this.HasProperty(key)) {
returnValue = ConfigurationManager.AppSettings[key];
} // end if
return returnValue;
} // end method ReadProperty
} // end class WebconfigurationPropertyService
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.