【问题标题】:MVC 3 getting values from AppSettings in web.configMVC 3 从 web.config 中的 AppSettings 获取值
【发布时间】:2013-10-25 17:41:06
【问题描述】:

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

首先,有 2 个 web.config 文件。一个在站点的根目录中,第二个列在“视图”区域中。我假设我想将我的 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"];
                }
            }
        }

【问题讨论】:

  • 1) 在根级别使用 web.config。 2) 您的set 实现不正确。正如您现在所拥有的那样,您的 clientid 不会被初始化,直到您明确调用 ServiceConfiguration.ClientID = ... 之后才会从 AppSettings 为其分配一个值。
  • 您的问题实际上是我正在寻找的答案,谢谢!

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4


【解决方案1】:

通常我使用 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);
    }
}

【讨论】:

  • 从 Web.config 文件的 appSettings 元素中读取的值始终为字符串类型。如果 Web.config 文件中不存在指定的键,则不会发生错误。而是返回一个空字符串。 (msdn.microsoft.com/en-us/library/610xe886(v=vs.85).aspx)
  • 我的意思是:我相信你应该检查 String.IsNullOrEmpty(value) 而不是 value = null
  • 没关系:我注意到您使用的是 System.Configuration.ConfigurationManager 而不是 System.Web.Configuration.WebConfigurationManager
【解决方案2】:

你有没有打电话给 set ?我猜它永远不会被调用,所以私有变量永远不会从配置中获取值。

试试这种方式(只取get中的值,不需要set):

public string ClientSecret
{    
  get { return System.Configuration.ConfigurationManager.AppSettings["ClientSecret"]; }    
}

【讨论】:

    【解决方案3】:

    我是这样做的:

    myVar = System.Configuration.ConfigurationManager.AppSettings["ClientID"].ToString();
    

    【讨论】:

      【解决方案4】:

      查看我假设您正在使用共享点提供商托管应用程序的代码? 在 mvc 中最好的做法是忽略视图级别的 web.config,只使用 web 应用程序根目录中的那个。

      我要提到的另一件事是,从实际模型中的 web.config 获取配置信息可能不是一个好主意。 最好将其移动到: - 控制器的构造函数 - 返回此模型的工厂/存储库

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-07
        • 2011-09-11
        • 2017-02-17
        • 1970-01-01
        • 1970-01-01
        • 2021-01-05
        • 1970-01-01
        相关资源
        最近更新 更多