Wednesday, 17 July 2013

Web API Formatters

     public static class FormattersConfig
    {
        public static void Register(HttpConfiguration config)
        {
            #region Json Formatter

            // Default formatter is Json Formatter
            // Default Json Formatter uses Json.Net
            // You can change the Json Formatter to use DataContractJsonSerializer
            config.Formatters.JsonFormatter.UseDataContractJsonSerializer = true;

            // How to Remove?
            config.Formatters.Remove(config.Formatters.JsonFormatter);

            #endregion

             #region Xml Formatter

            // Default Xml Formatter uses DataContractSerializer
            // You can change the Xml Formatter to XmlSerializer
            config.Formatters.XmlFormatter.UseXmlSerializer = true; 

            // How to Remove?
            config.Formatters.Remove(config.Formatters.XmlFormatter);

            #endregion

            #region Custom Formatter, custom Xml, comma delimited values, etc

            // Create CustomFormatter class which extends MediaTypeFormatter class.
            // Then call config.Formatters.Add(new CustomFormatter());
            // http://serena-yeoh.blogspot.co.uk/2013/02/aspnet-web-api-custom-formatter.html#!/2013/02/aspnet-web-api-custom-formatter.html

            #endregion
        }

No comments: