user681119
user681119

Reputation: 13

ConfigurationManager.AppSettings or only AppSettings?

I have question: when I to use <%= ConfigurationManager.AppSettings["xxx"] %> and <%$ AppSettings: xxx %>.

Sometimes when I use <%= ConfigurationManager.AppSettings["xxx "] %> a I got the following error: "Server tags cannot contain <% … %> constructs". Then a put <%$ AppSettings: xxx %> and it works.

Like this example: Error:

<asp:Literal runat="server" ID="Literal9" Text="<%= ConfigurationManager.AppSettings["xxx"] %>"></asp:Literal>

Working:

<asp:Literal runat="server" ID="Literal9" Text='<%$ AppSettings: xxx %>'></asp:Literal>

Upvotes: 1

Views: 822

Answers (1)

Kon
Kon

Reputation: 27451

The error occurs not because you're switching between ConfigurationManager.AppSettings and AppSettings, but because of the symbol used after <%. You can't have code rendering markup inside a server-side control that renders markup. The second way works because it evaluates the expression prior to server-side control render.

My preference is to always use ConfigurationManager.AppSettings, because it's more clear as to what the code is accessing.

Upvotes: 3

Related Questions