簡體   English   中英

appSettings和ConfigurationManager.AppSettings問題

[英]appSettings and ConfigurationManager.AppSettings issue

我搜索了網站,雖然我發現了一些非常有用的信息,但我無法弄清楚我的代碼是怎么回事。 我有以下web.config:

<?xml version="1.0"?>
<configuration>
  <system.web>
  </system.web>

  <system.webServer>
  </system.webServer>

  <appSettings>
    <add key="APIKey" value="23e24c73feed7ca0f6afd876575842de"/>
    <add key="Secret" value="################################"/>
    <add key="Callback" value="http://localhost:55994/"/>
    <add key="Suffix" value="My3Words"/>
  </appSettings>
</configuration>

我已經刪除了system.web和system.webServer中的內容,但它是ASP.NET MVC應用程序中生成的默認設置。

我試圖訪問該部分中的鍵(這是一個使用FB Connect的簡單Facebook應用程序)。

在我的代碼中,我有以下行:

return ConfigurationManager.AppSettings["APIKey"];

它返回null。 我無法弄清楚發生了什么。 我有必要的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Configuration;

在我的.cs文件的頂部。 我有一個非常強烈的懷疑,鍵盤后(即在我的大腦中)存在錯誤,但我無法解決這個問題。 有任何想法嗎?

您是否嘗試過使用WebConfigurationManager

return System.Web.Configuration.WebConfigurationManager.AppSettings["APIKey"];

這是在Web應用程序中使用配置文件的首選選項 - 它處理嵌套配置文件等內容。

Visual Studio為MVC創建了2個web.config文件。 1位於根目錄,另一位位於Views文件夾中。

你確定你使用的是正確的Web.Config文件嗎? 您可能希望使用應用程序根目錄中的那個,而不是Views目錄中的那個。

ASP.NET MVC和兩個Web.config文件

檢查以確保web.config文件的Build Action為“None”。 如果構建操作是“嵌入式資源”,我已經看到了這個問題。

您的machine.config是否具有必需的AppSettings部分。 它應該看起來像(版本號會有所不同):

<configuration>
   <configSections>
       <section name="appSettings"
          type="System.Configuration.NameValueFileSectionHandler, System,          Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
   </configSections>
</configuration>

你確定你使用的是正確的web.config嗎? ConfigurationManager.AppSettings.Settings是什么樣的?

我在我的解決方案中有兩個項目我首先在類庫項目中添加app.config文件,所有實例都是從控制台應用程序調用的,我在類lib項目的配置文件中添加了這些條目

<appSettings> 
<add key="userName" value="user2" /> 
<add key="emilsLimit" value="50" /> 
</appSettings>

當我在類庫項目中的類中獲取這些時,它會拋出null異常但是當我從類庫項目中刪除app.config並在Console項目中添加它時它會工作。

注意:在控制台中添加了類lib項目引用

我敢打賭你在編寫時使用正確的語法來訪問它們:ConfigurationManager.AppSettings [“Key1”];

嘗試使用此配置的格式

<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings>
    <add key="Key1" value="Val1"/>
    <add key="Key2" value="Val2"/>
</appSettings>
<connectionStrings>
    ......
    ......
</connectionStrings>
<system.web>
    <sessionState mode="InProc" timeout="20"/>
    <authorization>
        ......
    </authorization>

    <pages>
        <namespaces>
            <add namespace="System.Data"/>
            <add namespace="System.Data.SqlClient"/>
            <add namespace="System.IO"/>
        </namespaces>
    </pages>
    <customErrors mode="Off"/>
    <compilation debug="true"/></system.web>
</configuration>

你有沒有嘗試過:

return ConfigurationManager.AppSettings.Get("APIKey");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM