imin
imin

Reputation: 4578

read webConfigurationManager.AppSettings from asp.net

I have an asp.net page where I need to read some appSetting value from web.config. After googling, I found that I can use this code:

using System.Web.Configuration;

WebConfigurationManager.AppSettings["configFile"]

but after I insert the line using System.Web.Configuration by typing in this at my aspx page

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

I got this error: ASP.net runtime error. Only content controls are allowed directly in a content page that contains Content control. So how can I read the system web configuration file from my asp page if it doesn't allow me to import System.Web.Configuration?

Upvotes: 0

Views: 5977

Answers (1)

Dave Becker
Dave Becker

Reputation: 1433

I think this should do the trick (I've never used WebConfigutationManager but this works for me).

Code Behind

using System.Configuration

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

Aspx Page

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

Web.Config

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

EDIT:: You may need to add a reference to System.Configuration

Upvotes: 3

Related Questions