Error parsing AppSettings value with a query string

霸气de小男生 提交于 2020-01-01 01:35:13

问题


In my AppSettings in web.config, I have something like this:

<appSettings>
    <add key="ExternalSystemUrl" value="http://domain.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?


回答1:


Replace & with &amp; (escape it):

<add
    key="ExternalSystemUrl"
    value="http://domain.com/page.aspx?id={0}&amp;action=eat&amp;object=bacon" />

That's the common requirement for any valid XML file.

See Where can I get a list of the XML document escape characters?




回答2:


You can Try using &amp; instead.




回答3:


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 &amp; for your ampersand within the XML.



来源:https://stackoverflow.com/questions/6306399/error-parsing-appsettings-value-with-a-query-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!