Wednesday 15 August 2018

asp.net web api - Character set to send UK pound £ symbol to html client with webapi



In the database I have a NVARCHAR(100) field which has a UK £ symbol. I added the following in the web.config of the WebAPI application:






requestEncoding="iso-8859-1"
responseEncoding="iso-8859-1"/>




In the html page, I added the following to the head of the html:







I expected the £ to display correctly, but does not. It shows as ?



On the client side, I can't use string replace to replace the encoded £ to £ because I have so many content with this symbol.



Any ideas?




Update:



This is what I still see �. As per this page it is a REPLACEMENT CHARACTER. I copied the text in notepad.exe and tried to save as unicode html page, apply the ISO-8859-1 charset which also did not help.


Answer



The problem was related to using the correct character set of UTF-8. WebAPI by default uses utf-8.



HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8

Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcdGVtcFxUZXN0UG91bmRcVGVzdFBvdW5kXEFwaVxWYWx1ZXNcMQ==?=
X-Powered-By: ASP.NET
Date: Thu, 16 Jul 2015 21:25:24 GMT
Content-Length: 13


The response like above show the charset as utf-8. However, this can be overridden by using custom MediaTypeFormatter. I had a custom MediaTypeFormatter which was manipulating the data before sent to the client. After the custom formatter was removed, I can see the £ in the browser.



No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...