How to change default Web API 2 to JSON formatter?

后端 未结 7 1116
面向向阳花
面向向阳花 2020-12-13 14:40

I have a Web API project that returns some product data. It negotiates the return type correctly depending on the Accept header (JSON/XML) of the request. The problem is, if

相关标签:
7条回答
  • 2020-12-13 14:50

    I think you should change as following. Global.asax:

    /*For Indented formatting:*/       
     GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
    
        /*Response as default json format
         * example (http://localhost:9090/WebApp/api/user/)
         */
        GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
    
        /*Response as json format depend on request type
         * http://localhost:9090/WebApp/api/user/?type=json
         */
        GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(
        new QueryStringMapping("type", "json", new MediaTypeHeaderValue("application/json")));
    
        /*Response as xml format depend on request type
         * http://localhost:9090/WebApp/api/user/?type=xml
         */
        GlobalConfiguration.Configuration.Formatters.XmlFormatter.MediaTypeMappings.Add(
        new QueryStringMapping("type", "xml", new MediaTypeHeaderValue("application/xml")));
    
    0 讨论(0)
  • 2020-12-13 14:50
    config.EnableSystemDiagnosticsTracing();
    
    GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
    
    // Adding formatter for Json   
    config.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("type", "json", new MediaTypeHeaderValue("application/json")));
    
    // Adding formatter for XML   
    config.Formatters.XmlFormatter.MediaTypeMappings.Add(new QueryStringMapping("type", "xml", new MediaTypeHeaderValue("application/xml")));
    
    0 讨论(0)
  • 2020-12-13 14:54

    Or just remove the XmlFormatter

    var formatters = GlobalConfiguration.Configuration.Formatters;
    formatters.Remove(formatters.XmlFormatter);
    
    0 讨论(0)
  • 2020-12-13 14:56

    None of the above answers worked for me. The problem was that I was getting hold of the formatters from GlobalConfiguration and not the config object created with new HttpConfiguration() Here is the code that works for me :

    public class WebApiConfig
    {
        public static HttpConfiguration Register()
        {
    
            var config = new HttpConfiguration();
            // This next line could stay if you want xml formatting
            config.Formatters.Remove(config.Formatters.XmlFormatter);
    
            // This next commented out line was causing the problem
            //var jsonFormatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
    
            // This next line was the solution
            var jsonFormatter = config.Formatters.JsonFormatter;
            jsonFormatter.UseDataContractJsonSerializer = false; // defaults to false, but no harm done
            jsonFormatter.SerializerSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
            jsonFormatter.SerializerSettings.Formatting = Formatting.None;
            jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();           
    
            // remaining irrelevant code commented out
            return config;
        }
    }
    
    0 讨论(0)
  • 2020-12-13 15:03

    Add this in your App_Start/WebApiConfig.cs:

    config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
    
    0 讨论(0)
  • 2020-12-13 15:03

    I think Web API just uses the first formatter it can find in the Formatters collection. You can change the ordering with something like

    GlobalConfiguration.Configuration.Formatters.Clear();
    GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter());
    GlobalConfiguration.Configuration.Formatters.Add(new XmlMediaTypeFormatter());
    

    But it seems the JSON formatter should be the first one by default so you might want to check if you're already modifying this collection somewhere.

    0 讨论(0)
提交回复
热议问题