如何在ASPX项目的配置管理器中设置AppSettings

时间:2013-04-05 00:45:56

标签: c# asp.net configuration

这是一个使用Visual Studio 2010的ASPX / CS项目。这是一个Configuration Manager问题。

我正在成功调试(排序)已在服务器上使用的一些代码。但是有一段代码与实时版本中的URL一起使用,不应该在debug / localhost版本中使用。

 protected void Page_Load(object sender, EventArgs e)
    {
        if (ConfigurationManager.AppSettings["IsTesting"] == "false" && Request.Url.ToString().Contains("http:"))
        {
            Response.Redirect(Request.Url.ToString().Replace("http:", "https:"));
        }

        LoadMasterTemplate();
    }

这段代码落在“Response.Redirect ....”行上,因为“IsTesting”应用程序设置应该在ConfigurationManager中设置为true。我该如何设置?

2 个答案:

答案 0 :(得分:1)

在App / Web.config文件中的<configuration>元素内,应该(或者您应该创建)<appSettings></appSettings>标记,并且各个设置看起来像这样:

<appSettings>
  <add key="NewKey0" value="Something1" />
  <add key="NewKey1" value="Something2" />
</appSettings>

答案 1 :(得分:1)

“ConfigurationManager”查看ASP.Net解决方案的“Web.Config”,因此您可以在以下位置找到它:

<configuration>
  <appSettings>
    <add key="IsTesting" value="true"/>
  </appSettings>    
</configuration>

或者,如果您访问IIS管理器并选择网站,然后单击“应用程序设置”,您可以通过GUI更改它。