默认 JSON 格式化效率以可读性为代价

让我们说你有一个像这样的简单 ApiController:

    [HttpGet]
    [Route("test")]
    public dynamic Test()
    {
        dynamic obj = new ExpandoObject();
        obj.prop1 = "some string";
        obj.prop2 = 11;
        obj.prop3 = "another string";

        return obj;
    }

此对象的结果 JSON 表示将如下所示:

{"prop1":"some string","prop2":11,"prop3":"another string"}

对于像这样的简单响应,这可能很好,但想象一下,如果你有一个大/复杂的对象作为响应发送:

"response": { "version": "0.1", "termsofService": "http://www.wunderground.com/weather/api/d/terms.html", "features": { "history": 1 } }, "history": { "date": { "pretty": "July 16, 2016", "year": "2016", "mon": "07", "mday": "16", "hour": "12", "min": "00", "tzname": "America/Indianapolis" }, "utcdate": { "pretty": "July 16, 2016", "year": "2016", "mon": "07", "mday": "16", "hour": "16", "min": "00", "tzname": "UTC" }, "observations": [{ "date": { "pretty": "12:15 AM EDT on July 16, 2016", "year": "2016", "mon": "07", "mday": "16", "hour": "00", "min": "15", "tzname": "America/Indianapolis" }, "utcdate": { "pretty": "4:15 AM GMT on July 16, 2016", "year": "2016", "mon": "07", "mday": "16", "hour": "04", "min": "15", "tzname": "UTC" }, "tempm": "18.2", "tempi": "64.8", "dewptm": "16.4", "dewpti": "61.5", "hum": "89", "wspdm": "9.3", "wspdi": "5.8", "wgustm": "-9999.0", "wgusti": "-9999.0", "wdird": "20", "wdire": "NNE", "vism": "16.1", "visi": "10.0", "pressurem": "1018.2", "pressurei": "30.07", "windchillm": "-999", "windchilli": "-999", "heatindexm": "-9999", "heatindexi": "-9999", "precipm": "-9999.00", "precipi": "-9999.00", "conds": "Clear", "icon": "clear", "fog": "0", "rain": "0", "snow": "0", "hail": "0", "thunder": "0", "tornado": "0", "metar": "METAR KTYQ 160415Z AUTO 02005KT 10SM CLR 18/16 A3007 RMK AO2 T01820164" }, { "date": { "pretty": "12:35 AM EDT on July 16, 2016", "year": "2016", "mon": "07", "mday": "16", "hour": "00", "min": "35", "tzname": "America/Indianapolis" }, "utcdate": { "pretty": "4:35 AM GMT on July 16, 2016", "year": "2016", "mon": "07", "mday": "16", "hour": "04", "min": "35", "tzname": "UTC" }, "tempm": "17.7", "tempi": "63.9", "dewptm": "16.3", "dewpti": "61.3", "hum": "91", "wspdm": "7.4", "wspdi": "4.6", "wgustm": "-9999.0", "wgusti": "-9999.0", "wdird": "10", "wdire": "North", "vism": "16.1", "visi": "10.0", "pressurem": "1018.2", "pressurei": "30.07", "windchillm": "-999", "windchilli": "-999", "heatindexm": "-9999", "heatindexi": "-9999", "precipm": "-9999.00", "precipi": "-9999.00", "conds": "Clear", "icon": "clear", "fog": "0", "rain": "0", "snow": "0", "hail": "0", "thunder": "0", "tornado": "0", "metar": "METAR KTYQ 160435Z AUTO 01004KT 10SM CLR 18/16 A3007 RMK AO2 T01770163" } } }

这不是你认为高度可读的数据。通过在 App_Start / ApiConfig.cs 中设置默认 JsonFormatter 上的单个属性,可以轻松解决这个问题:

    // Either one of these will format your JSON in a readable format.  Setting both provides no additional benefit.
    config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
    // OR
    config.Formatters.JsonFormatter.Indent = true;