ConfigurationManager.AppSettings 还是仅 AppSettings?
我有问题:当我使用 <%= ConfigurationManager.AppSettings["xxx"] %>
和 <%$ 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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生错误的原因不是您在
ConfigurationManager.AppSettings
和AppSettings
之间切换,而是因为<%
之后使用了符号。您不能在呈现标记的服务器端控件内使用代码呈现标记。第二种方法有效,因为它在服务器端控件呈现之前评估表达式。我的偏好是始终使用
ConfigurationManager.AppSettings
,因为这样可以更清楚地了解代码正在访问的内容。The error occurs not because you're switching between
ConfigurationManager.AppSettings
andAppSettings
, 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.