zoukankan      html  css  js  c++  java
  • asp.net web api 安装swagger

    使用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出现“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))
  • 相关阅读:
    problem in Sourcetree
    Get started with Sourcetree
    IIS application pool access desktop denied
    结构型模式 适配器模式
    结构型模式 装饰模式
    结构型模式 代理模式
    创建型模式 原型模式
    创建型模式 建造者模式
    创建型模式 抽象工厂
    设计模式的六大原则
  • 原文地址:https://www.cnblogs.com/smileberry/p/7305567.html
Copyright © 2011-2022 走看看