使用查询字符串解析 AppSettings 值时出错
在 web.config
的 AppSettings 中,我有这样的内容:
<appSettings>
<add key="ExternalSystemUrl" value="http://example.com/page.aspx?id={0}&action=eat&object=bacon" />
</appSettings>
但是,似乎当 AppSettings 值中包含与号 (&
) 时,ASP.NET抛出以下错误:
解析 EntityName 时发生错误
为什么会发生这种情况?如何在 App.config
中包含这样的 URL?
In my AppSettings in web.config
, I have something like this:
<appSettings>
<add key="ExternalSystemUrl" value="http://example.com/page.aspx?id={0}&action=eat&object=bacon" />
</appSettings>
However, it seems that when an ampersand (&
) is included in an AppSettings value, ASP.NET throws the following error:
An error occurred while parsing EntityName
Why does this happen, and how can I include URLs like this in App.config
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将
&
替换为&
(转义):这是任何有效 XML 文件的常见要求。
请参阅 在哪里可以获得以下列表XML 文档转义字符?
Replace
&
with&
(escape it):That's the common requirement for any valid XML file.
See Where can I get a list of the XML document escape characters?
您可以尝试使用
&
代替。You can Try using
&
instead.在 XML 中,& 符号告诉解析器“紧跟在该 & 符号后面的数据是需要翻译的实体”。如果紧随其后的数据不是有效的 XML 实体,则会出现此错误。如果可能,请在 XML 中使用
&
作为 & 符号。In XML an ampersand tells the parser "the data immediately following this ampersand is an entity which needs to be translated." If the data immediately following is not a valid XML entity, then you get this error. If possible, use
&
for your ampersand within the XML.