zoukankan      html  css  js  c++  java
  • Asp.Net Core WebApi中接入Swagger组件(初级)

    开发WebApi时通常需要为调用我们Api的客户端提供说明文档。Swagger便是为此而存在的,能够提供在线调用、调试的功能和API文档界面。

    环境介绍:Asp.Net Core WebApi + Swagger。

    一、加入Swagger组件

      Swashbuckle.AspNetCore 中分为3个组件。

    PackageDescription
    Swashbuckle.AspNetCore.Swagger Exposes SwaggerDocument objects as a JSON API. It expects an implementation of ISwaggerProvider to be registered which it queries to retrieve Swagger document(s) before returning as serialized JSON
    Swashbuckle.AspNetCore.SwaggerGen Injects an implementation of ISwaggerProvider that can be used by the above component. This particular implementation automatically generates SwaggerDocument(s) from your routes, controllers and models
    Swashbuckle.AspNetCore.SwaggerUI

    Exposes an embedded version of the swagger-ui. You specify the API endpoints where it can obtain Swagger JSON and it uses them to power interactive docs for your API、

      

      安装Swashbuckle.AspNetCore,在Nuget中搜索或是包管理器下输入命令都可,找到这个包,安装,其中有三个小弟是三个不同的组件,在第一个中都集成了。

     二、加入到Asp.Net Core管道中,并注入服务

      加入Swagger中间件

     1     public void Configure(IApplicationBuilder app, IHostingEnvironment env)
     2     {
     3         if (env.IsDevelopment())
     4         {
     5             app.UseDeveloperExceptionPage();
     6         }
     7 
     8         app.UseSwagger();
     9         app.UseSwaggerUI(c =>
    10         {
    11             c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
    12         });
    13 
    14         app.UseMvc();
    15     }

       加入Swagger服务

    1     public void ConfigureServices(IServiceCollection services)
    2     {
    3         services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    4 
    5         services.AddSwaggerGen(options =>
    6         {
    7             options.SwaggerDoc("v1", new Info { Title = "My WebApi Document", Version = "v1" });
    8         });
    9     }

    三、启动WebApi即可,注意访问路径

    2018-08-18,望技术有成后能回来看见自己的脚步
  • 相关阅读:
    android 源码下载(Windows+Linux)
    Android Studio依赖包冲突 Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
    深入了解Java--1 绪论
    Android Studio 找不到R文件解决方法汇总
    Git学习历程
    word自动生成目录左对齐(缩进)问题
    Android Studio simpleUML(UML工具)使用详解
    android studio 常用快捷键
    当我们提起“女性权益”的时候,我们到底指的是什么?
    weakref模块和弱引用
  • 原文地址:https://www.cnblogs.com/CKExp/p/9496379.html
Copyright © 2011-2022 走看看