1

我有一个问题:我什么时候使用<%= ConfigurationManager.AppSettings["xxx"] %>and <%$ AppSettings: xxx %>

有时,当我使用<%= ConfigurationManager.AppSettings["xxx "] %>a 时,会出现以下错误:“服务器标签不能包含 <% ... %> 构造”。然后一个看跌<%$ AppSettings: xxx %>,它的工作原理。

像这个例子:错误:

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

在职的:

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

1 回答 1

3

发生错误不是因为您在 and 之间切换ConfigurationManager.AppSettingsAppSettings而是因为 . 之后使用的符号<%。您不能在呈现标记的服务器端控件中使用代码呈现标记。第二种方法有效,因为它在服务器端控件渲染之前评估表达式。

我的偏好是始终使用ConfigurationManager.AppSettings,因为它更清楚代码正在访问什么。

于 2011-03-29T00:07:03.697 回答