zoukankan      html  css  js  c++  java
  • C#中使用swagger小技巧

    C#中使用swagger小技巧 

    swaggerUI显示的接口内容主要用于开发阶段便于与前端联调,不适合发布到对外的站点。

    有以下两种方式,让接口不显示在SwaggerUI中

    1.使用属性 [ApiExplorerSettings(IgnoreApi = true)]设置接口不显示到swaggerUI,此方法比较灵活,可以根据情况在对于的控制器或者API接口中添加。

    列如:

        [ApiExplorerSettings(IgnoreApi = true)]

        public class SystemController : ApiController

        {   

    }

    这样编写,整个控制器中的接口都不会显示在swaggerUI,如果只是想针对某个接口不显示在UI中,可以在接口处增加属性,列如:

        public class SystemController : ApiController

        {

            [HttpGet]

            [ApiExplorerSettings(IgnoreApi = true)]

            public string GetSystemTime()

            {

                return DateTime.Now.ToString();

            }       

    }

    我们调试一般都会将编译类型选择为Debug,那么可以如下写:

    #if (!DEBUG)

        [ApiExplorerSettings(IgnoreApi = true)]

     #endif

    public class SystemController : ApiController

    {   

    }

    这样开发阶段就可以正常的查看swaggerUI,到发布到UAT或者其他环境时,选择Release类型编译发布即可。

    针对某个接口不显示在UI中,可以在接口处修改属性,列如:

        public class SystemController : ApiController

        {

            [HttpGet]

            #if (!DEBUG)

                [ApiExplorerSettings(IgnoreApi = true)]

    #endif

            public string GetSystemTime()

            {

                return DateTime.Now.ToString();

          }       

    }

    第二种方式:

    就是不加载SwaggerConfig文件,这样在发布就不会显示swaggerUI,列如:

    #if (DEBUG)

        [assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]

    #endif

    这样做的缺点是,接口说明都不显示了。

    可以通过自己的使用情况,选择合适的方式。

    寻寻觅觅转流年,磕磕碰碰道缘浅。 揽几缕、轻挽起,暮暮朝朝与君语。
  • 相关阅读:
    CentOS 出错处理
    g13 root
    修复误删系统文件
    c++ list sort方法
    批量修改outlook联系人头像,并同步手机
    IT大牛们 学术搜索
    oracle 表空间操作
    CentOS 5.5 安装MPICH2\MRNet\Launchmon时遇到的问题
    自我反省
    积累航程
  • 原文地址:https://www.cnblogs.com/bingshao/p/13606670.html
Copyright © 2011-2022 走看看