ASP.NET Web API content encoding

S.. picture S.. · Jan 30, 2014 · Viewed 12.2k times · Source

I've created an API.NET Web API. It uses the default utf-8 content encoding. I need the output to be in latin-1 to match the database.

Answer

Gabriel Monteiro Nepomuceno picture Gabriel Monteiro Nepomuceno · Jan 30, 2014

There is a article here that shows how to do this http://blogs.msdn.com/b/henrikn/archive/2012/04/22/asp-net-web-api-content-negotiation-and-accept-charset.aspx

You just need to change from this article to remove all the encode suported and include just latin-1

something like this in your app start

   protected void Application_Start()
   {

       Encoding latinEncoding = Encoding.GetEncoding("Latin-1");
       GlobalConfiguration.Configuration.Formatters.JsonFormatter.SupportedEncodings.Add(latinEncoding);
       GlobalConfiguration.Configuration.Formatters.JsonFormatter.SupportedEncodings.RemoveAt(0);

   } 

And add the below namespace:

using System.Text;