Search code examples
asp.netweb-servicesasp.net-web-apicontent-typecontent-encoding

ASP.NET Web API content encoding


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.


Solution

  • 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;