asp.net-mvc MVC 3 从 web.config 中的 AppSettings 获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19596233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:15:47  来源:igfitidea点击:

MVC 3 getting values from AppSettings in web.config

asp.net-mvcasp.net-mvc-3asp.net-mvc-4

提问by Vinod Kumar

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.

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

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.)

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

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?

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

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?

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

In a Controller.cs:

在 Controller.cs 中:

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

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

In web.config

在 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>

...

In the Model.cs file:

在 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"];
                }
            }
        }

回答by Stanislav Nedeljkovic

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

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

回答by Phil Sandler

Are you ever calling set? I'm guessing it never gets called, so the private variable never gets the value from the config.

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

Try it this way (just retrieve the value in the get, no set needed):

以这种方式尝试(只需检索 get 中的值,无需设置):

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

回答by Allfarid Morales García

I did it this way:

我是这样做的:

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

回答by Serge

Looking at the code I assume you are using sharepoint provider hosted apps? Best thing to do here in mvc is to ignore the web.config which is on the view level and only use the one in the root of the webapplication.

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

The other thing I want to mention is that its probably not a good idea to fetch configuration information from the web.config in the actual model. Its better to move it either to : - the constructor of the controller - the factory/repository which returns this model

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