zoukankan      html  css  js  c++  java
  • net core --- Swagger搭建(net core 3.1 版本)

    Swagger是一个API接口文档帮助插件,使用它,可以将我们编写的接口自动生成一个规范化的文档,其他开发人员(主要是负责的对接接口的前端人员)就可以通过浏览器访问对应的地址,查看接口的相关信息。

    添加 Swashbuckle.AspNetCore NuGet包

    安装完成之后,编辑Startup.cs文件

    在ConfigureServices方法里加入下面的代码,注册Swagger生成器,定义一个文档,设置xml文档的注释路径。

    备注:  net core 3.0以下为 new Info 

    //配置Swagger
                //注册Swagger生成器,定义一个Swagger 文档
                services.AddSwaggerGen(c =>
                {
                    c.SwaggerDoc("v1", new OpenApiInfo
                    {
                        Version = "v1",
                        Title = "接口文档",
                        Description = "RESTful API"
                    });
                    // 为 Swagger 设置xml文档注释路径
                    var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                    var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                    c.IncludeXmlComments(xmlPath);
                });

    在Configure方法里加入下面的代码,启用中间件服务使用生成Swagger和SwaggerUI,将SwaggerUI中的RoutePrefix设为空字符串,这样就能在根节点(http://localhost:port)直接显示SwaggerUI界面。

    //启用中间件服务生成Swagger
    app.UseSwagger();
    //启用中间件服务生成SwaggerUI,指定Swagger JSON终结点
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "Web App V1");
        c.RoutePrefix = string.Empty;//设置根节点访问
    });

    完整 Startup

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.HttpsPolicy;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    using Microsoft.Extensions.Logging;
    using System.IO;
    using System.Reflection;
    using Swashbuckle.AspNetCore.Swagger;
    using Microsoft.OpenApi.Models;
    
    namespace coreWebApiDemo
    {
        public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddControllers();
    
    
                
    
                //配置Swagger
                //注册Swagger生成器,定义一个Swagger 文档
                services.AddSwaggerGen(c =>
                {
                    c.SwaggerDoc("v1", new OpenApiInfo
                    {
                        Version = "v1",
                        Title = "接口文档",
                        Description = "RESTful API"
                    });
                    // 为 Swagger 设置xml文档注释路径
                    var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                    var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                    c.IncludeXmlComments(xmlPath);
                });
    
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
    
                //启用中间件服务生成Swagger
                app.UseSwagger();
                //启用中间件服务生成SwaggerUI,指定Swagger JSON终结点
                app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "Web App V1");
                    c.RoutePrefix = string.Empty;//设置根节点访问
                });
    
    
                app.UseHttpsRedirection();
    
                app.UseRouting();
    
                app.UseAuthorization();
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers();
                });
            }
        }
    }
    View Code

    然后,右键项目,点击属性。

    选择生成,选择我们的Debug路径。

    勾选XML文档文件,自动填充。

    因为配置好了swagger后默认启动就会调用,所以把默认启动页面关掉防止冲突。

    效果:

    调用一下接口

    接口 get

     

  • 相关阅读:
    iptables 端口转发规则
    iptables 设置端口转发/映射
    iptables 从一台机到另一台机端口转发
    iptables nat 技术转发
    统计将⽂文件内容读出,然后统计读出的字符串串中每个字符的个数
    day03-课堂笔记-大纲
    day03-课堂笔记
    转载:linux同步时间信息
    获取列表的索引操作:enumerate
    oldboy-作业01.登录多次进行账号锁定
  • 原文地址:https://www.cnblogs.com/JoeYD/p/14944829.html
Copyright © 2011-2022 走看看