asp.net-mvc – MVC 3从web.config中的AppSettings获取值
发布时间:2020-12-16 00:43:37 所属栏目:asp.Net 来源:网络整理
导读:在正常的ASP.NET Web表单站点中,我将使用web.configs“appsettings”将应用程序设置数据添加到站点。但是,当使用MVC 3时,我无法以这种方式检索设置值。 首先,有2个web.config文件。一个在网站的根目录,第二个列在Views区域。我假设我想把我的appsetting
在正常的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-mvc – 在nopcommerce 2.8中使用Telerik插件的自定
- asp.net-mvc-5 – aspnet身份避免同时登录同一帐户
- asp.net-mvc – 使用类似MvcContrib Grid的东西在代码可读性
- asp.net-mvc – 属性路由和本地化问题
- asp.net core webapi 统一处理返回值、异常和请求参数验证
- asp.net-mvc – 在VS 2015中添加视图上下文菜单非常慢
- ASP.NET AJAX中的$create函数是什么?
- asp.net – Session实际上如何在MVC中运行?
- asp.net-mvc – ASP.NET MVC路由 – 所有看起来都匹配正确的
- 在ASP.NET Identity 2.0.0-beta1中禁用双因素身份验证
推荐文章
站长推荐
- Asp.net Core +MVC+Bootstrap开发项目实站2
- asp.net – 使用ASP .Net MVC4处理Web API中的集
- 如何在Asp.Net Web API 2中使用Owin OAuth2修改令
- asp.net – 为什么页脚项目不包含在Repeater.Ite
- asp.net-mvc – 在布局页面上渲染脚本时出现奇怪
- asp.net – 实体框架和MVC应用程序中的奇怪编译错
- asp.net-mvc-2 – Asp.net Mvc显示String的模板,
- asp.net-mvc-3 – asp.net中的pageBaseType mvc3
- 如何通过asp.net mvc或jquery提交包含动态数据行
- asp.net – 如何为外部依赖项使用依赖注入?
热点阅读