asp.net web api 安装swagger webapi使用swagger出现“Cannot read property 'parameters' of null”

asp.net web api 安装swagger
webapi使用swagger出现“Cannot read property 'parameters' of null”

使用nuget控制台,

 输入 Install-Package Swashbuckle,回车,等待安装引用。nuget国内没有镜像,安装比较慢

安装成功后会多出一个引用

右键工程点--属性,左边导航栏选择--生成,勾选--XML文档文件,XML名字可以起符合命名规范的任意值,Ctrl+S

引用Swagger工具后App_Start底下会多出一个SwaggerConfig.cs,点击打开

修改如下:

c.IncludeXmlComments(GetXmlCommentsPath());

   private static string GetXmlCommentsPath()
        {
            return System.String.Format(@"{0}inMDTController.XML", System.AppDomain.CurrentDomain.BaseDirectory);
        }

直接访问:http://localhost/Controller/swagger/ui/index#/

如有错误:

前端时间在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))