While using HTML entities, it also translates ampersand &

蓝咒 提交于 2019-12-11 18:46:48

问题


I am working on a ASP.NET MVC multi language web site.

When I put for example é to achieve this é, it changes it to é.

It seems that it first translates & to &.

What is the problem?


回答1:


Ok I think I might see what you are referring to. In the meta tags in the head of your document é is changing to é but the entities in the body are correct.

You have perhaps copied and pasted é from a rich text environment (i.e. an rtf or perhaps a .doc file) and it may contain an encoded ampersand rather than a plain text ampersand. Rule of thumb - never ever ever copy text from something like word as it copies across all sorts of invisible characters. When you prepare copy for your website always do in in plain text (.txt) files and you'll know your text is clean. If a client gives you .doc file convert them to .txt first and you will see any issues before you copy them into the html.

However, in the head of your document when you have text in an attribute value, what's between the quotes, for example in the content attribute of the description meta tag, like so:

<meta name="description" content="Enregistreur vocal est une application polyvalente avec un son de haute qualité." />

you don't need to use entities because that string is plain text not html - so change it to a normal é and it should work.

Note: The above assumes that the string in the meta tags are not coming from a database but is actually in the html page. If the string is coming from a database and then placed into the meta tags you may have used something like HttpContext.Current.Server.HtmlEncode() in asp to place it in the database in the first place - that's the root of the issue. If this is the case, before you place it in the meta tag you need to use HttpContext.Current.Server.Htmldecode() to convert it back to plain text.



来源:https://stackoverflow.com/questions/51685721/while-using-html-entities-it-also-translates-ampersand

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