MVC 3从web.config中的AppSettings获取值 [英] MVC 3 getting values from AppSettings in web.config

查看:146
本文介绍了MVC 3从web.config中的AppSettings获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在普通的ASP.NET Web窗体站点中,我将使用web.configs"appsettings"将应用程序设置数据添加到站点.但是,在使用MVC 3时,无法以这种方式检索设置值.

In normal ASP.NET Web forms sites I would use web.configs "appsettings" to add application setting data to a site. However, I am not able to retrieve setting values this way when using MVC 3.

首先,有2个web.config文件.一个在网站的根目录中,第二个在视图"区域中列出.我假设我想将我的appsettings信息放在web.config根文件中,对吗? (将其放在其他视图下似乎会产生错误,指出"AppSettings"在每个Web应用程序中只能出现一次.)

First off, there are 2 web.config files. One in the root of the site, the second is listed in the Views area. I assume I want to put my appsettings information in the root web.config file, correct? (putting it in the other under views seems to produce an error stating "AppSettings" can only appear once per web application.)

当我尝试检索它(C#:System.Configuration.ConfigurationManager.AppSettings ["SettingName"])时,我得到一个空白或空/空返回值.我在做什么错了?

When I try to retrieve it (C#: System.Configuration.ConfigurationManager.AppSettings["SettingName"]) I get a blank or empty/null return value. What am I doing wrong?

我应该提到,我实际上是在Models区域下的Class文件中检索此信息,以便使用get设置模型的特定值;放;.是否有可能不允许我在模型中执行此操作?

I should mention that I am actually retrieving this information in a Class file under the Models area for set specific values for a model using get; set;. Is it possible that I'm not allowed to do this in Models?

在Controller.cs中:

In a Controller.cs:

WindowsLiveConnect.ServiceConfiguration WLSC = new WindowsLiveConnect.ServiceConfiguration();

ViewBag.ClientID = SC.ClientID; // This returns empty

在web.config中

In 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文件中:

In the Model.cs file:

        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静态类访问这些参数.像这样:

Usually I'm using AppSettings static class to access those parameters. Something like this:

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);
    }
}

这篇关于MVC 3从web.config中的AppSettings获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆