2015-02-10 67 views
0

我有一個asp.net頁面,我需要從web.config中讀取一些appSetting值。谷歌搜索後,我發現我可以使用此代碼:我通過我的aspx頁面在此鍵入從asp.net閱讀webConfigurationManager.AppSettings

<%@ Using System.Web.Configuration; %> 

我得到這個錯誤插入使用System.Web.Configuration行後

using System.Web.Configuration; 

WebConfigurationManager.AppSettings["configFile"] 

但:ASP.net運行時錯誤。只有內容控件才能直接在包含Content控件的內容頁面中使用。那麼,如果我不能導入System.Web.Configuration,我怎麼能從我的asp頁面讀取系統web配置文件?

+0

是這個mvc或webforms? – 2015-02-10 08:12:41

+0

@DaveBecker這是一個網絡應用程序。其實我試圖改變Forgot my password href從web.config – imin 2015-02-10 08:19:44

+0

好的,我知道如何做到這一點,但我需要知道,如果它是一個mvc網絡應用程序(然後答案是基於模型)或webforms網絡應用程序(後面會有代碼)。 – 2015-02-10 08:26:50

回答

1

我認爲這應該做的伎倆(我從來沒有用過WebConfigutationManager,但這對我有用)。

代碼隱藏

using System.Configuration 

protected string GetForgottenUrl() 
{ 
    return ConfigurationManager.AppSettings["SomeKey"]; 
} 

ASPX頁面

<a href='<%= GetForgottenUrl()%>'>Forgot Password</a> 

的Web.Config

<configuration> 
    <appSettings> 
    <add key="SomeKey" value="SomePage.aspx" /> 
    </appSettings> 
</configuration> 

編輯::您可能需要添加一個參考System.Configuration