简体   繁体   中英

Html.Encode class not encoding ≥ or ≤ characters

I have an string that I need to convert it to XML and for the most part it is working for < or > but it is not for ≥ or ≤ characters.

Input string is:

Lorem ipsum dolor sit amet <65 Lorem ipsum dolor sit amet, ≥65 Lorem ipsum dolor sit amet

<span>@Html.Encode(indication.Sentence.CompiledTextTitleCase)</span>

Output string is:

Lorem ipsum dolor sit amete &lt;65 Lorem ipsum dolor sit amet ≥65 Lorem ipsum dolor sit amet

Any idea why this is happening any how to fix it?

while < and > have a special meaning in html (start/end for a tag, eg <title> ), thers nothing special about . This is why you have to encode the former, while the latter works as it is.

Having said this Html.Encode will only encode those characters which have a special meaning and thus can be mis-interpreted when reading that data. At client-side you get this code:

<span>Lorem ipsum dolor sit amete &lt;65 Lorem ipsum dolor sit amet ≥65 Lorem ipsum dolor sit amet</span>

where &lt;65 will be printed as < .

If you´d send the < un-encoded to your client you ´d get this:

<span>Lorem ipsum dolor sit amete <65 Lorem ipsum dolor sit amet ≥65 Lorem ipsum dolor sit amet</span>

which isn´t valid html for the reason mentioned above.

So there´s nothing to be fixed here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM