webapi使用swagger出现“Cannot read property 'parameters' of null”

前端时间在webapi项目使用swagger来提供接口文档及测试工具,按网上方法(http://wmpratt.com/swagger-and-asp-net-web-api-part-1配置好之后在浏览器控制台出现"Cannot read property 'parameters' of null"错误。

后面发现问题出在//localhost:5645/swagger/docs/v1这个JSON资源上面,序列化出来的JSON,包含了为NULL的字段,导致swagger-ui-min-js出现异常。

进一步分析是因为我项目使用的newtonsoft.json这个库的配置导致,应该忽略为NULL的字段,如下:

//这里使用自定义日期格式
var jsonFormatter = new JsonMediaTypeFormatter();
var settings = jsonFormatter.SerializerSettings;
var timeConverter= new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
settings.Converters.Add(timeConverter);
settings.NullValueHandling = NullValueHandling.Ignore;
config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter))
posted @ 2017-07-12 15:51  yingcheng1  阅读(4083)  评论(4编辑  收藏  举报