Jacob Rutherford
Jacob Rutherford

Reputation: 594

ASP.NET Web API and UTF16 encoding

I have a ASP.NET web API running and have a client running into following issue:

{"model":["Unable to translate bytes [E9] 
at index 300 from specified code page to Unicode."]}

The exception appears to be happening during model binding, which is the default nothing custom on my end.

The client is also running .NET and they say that the content is being sent over as UTF16....but they are not including HTTP header to indicate that. Up until they began having special characters in their content everything worked great.

My question is how to deal with this. Initially I was going to add a DelegateHandler which would detect this client and somehow set content-type/charset to UTF16 but that isn't panning out. I then thought I could transcode the request body to UTF8 from within DelegateHandler but have read that modifying content in handler is not possible or recommended.

Since the issue appears to be during model binding I'm wondering if implementing a custom model binder would be in order?

Thanks for any help!

Upvotes: 1

Views: 4940

Answers (1)

Guanxi
Guanxi

Reputation: 3131

The default encoding used by the built-in formatters is UTF-8. You can easily change it to UTF 16. You can change this on a per-formatter basis by changing the SupportEncodings property on the MediaTypeFormatter class.

This link can help you.

Upvotes: 3

Related Questions