asp.net-mvc – MVC 3从web.config中的AppSettings获取值
发布时间:2020-05-25 02:48:13 所属栏目:asp.Net 来源:互联网
导读:在正常的ASP.NET Web表单站点中,我将使用web.configs“appsettings”将应用程序设置数据添加到站点。但是,当使用MVC 3时,我无法以这种方式检索设置值。 首先,有2个web.config文件。一个在网站的根目录,第二个列在Views区域。我假设我想把我的appsettings
|
在正常的ASP.NET Web表单站点中,我将使用web.configs“appsettings”将应用程序设置数据添加到站点。但是,当使用MVC 3时,我无法以这种方式检索设置值。 首先,有2个web.config文件。一个在网站的根目录,第二个列在Views区域。我假设我想把我的appsettings信息放在根web.config文件中,是否正确? (把它放在另一个视图下似乎产生一个错误,指出“AppSettings”只能在每个Web应用程序出现一次。) 当我尝试检索它(C#:System.Configuration.ConfigurationManager.AppSettings [“SettingName”])我得到一个空白或空/空返回值。我究竟做错了什么? 我应该提到,我实际上是在模型区域的一个类文件中检索这个信息,使用get设置一个模型的具体值;组;。在模型中我不可能做到这一点吗? 在Controller.cs中: WindowsLiveConnect.ServiceConfiguration WLSC = new WindowsLiveConnect.ServiceConfiguration(); ViewBag.ClientID = SC.ClientID; // This returns empty 在web.config中 ...
<appSettings>
<add key="webpages:Version" value="1.0.0.0"/>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
<add key="ClientID" value="0000000040062A3F" />
<add key="ClientSecret" value="SUPERSECRETPASSWORD" />
<add key="RedirectURL" value="http%3A%2F%2Fwww.quilnet.com" />
</appSettings>
...
在Model.cs文件中: public class ServiceConfiguration
{
private string clientid;
private string clientsecret;
private string redirecturl;
public string ClientID
{
get { return clientid; }
set
{
clientid = System.Configuration.ConfigurationManager.AppSettings["ClientID"];
}
}
public string ClientSecret
{
get { return clientsecret; }
set
{
clientsecret = System.Configuration.ConfigurationManager.AppSettings["ClientSecret"];
}
}
public string RedirectURL
{
get { return redirecturl; }
set
{
redirecturl = System.Configuration.ConfigurationManager.AppSettings["RedirectURL"];
}
}
}
解决方法通常我正在使用AppSettings静态类来访问这些参数。这样的事情public static class AppSettings
{
public static string ClientSecret
{
get
{
return Setting<string>("ClientSecret");
}
}
private static T Setting<T>(string name)
{
string value = ConfigurationManager.AppSettings[name];
if (value == null)
{
throw new Exception(String.Format("Could not find setting '{0}',",name));
}
return (T)Convert.ChangeType(value,typeof(T),CultureInfo.InvariantCulture);
}
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-web-api – 使用swashbuckle api文档的http basic
- asp.net-mvc-3 – ViewBag/ViewData生命周期
- 最好的asp.net日历/日程安排组件?
- asp.net – 我可以在URL中使用逗号吗?
- remoting和webservice有什么区别
- asp.net-mvc – Kendo UI异步上传无法在Internet Explorer中
- asp.net-mvc – 单元测试中的ViewResult.ViewName属性为空
- asp.net-mvc – MVC应用程序调试错误:viewstate MAC的验证
- asp.net-mvc-3 – 在远程部署MVC3时获
- asp.net – System.Reflection.Assembly.LoadFile锁定文件
推荐文章
站长推荐
- asp.net-mvc – 在asp.net mvc请求中捕获windows
- asp.net – 在集成模式下替换HttpContext.Curren
- 使用什么方法将ASP.Net应用程序部署到野外?
- asp.net-mvc-3 – 使用接口作为部分视图数据注释
- asp.net – 您有任何免费的.Net托管代码将DocX转
- asp.net-mvc – ASP.NET MVC 3 – 你想看什么功能
- ASP.NET清空缓存时遇到的问题简析
- .net – 如何使用SmtpClient.SendAsync发送带有附
- 如何设置起始页面在webconfig文件在asp.net c#
- asp.net-mvc-2 – TempData未按预期清除
热点阅读
