Fork me on GitHub

swagger

1,添加引用

<GenerateDocumentationFile>true</GenerateDocumentationFile>
    <NoWarn>$(NoWarn);1591</NoWarn> 生成注释文件
<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
 <GenerateDocumentationFile>true</GenerateDocumentationFile>
    <NoWarn>$(NoWarn);1591</NoWarn>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.5.0" />
  </ItemGroup>


</Project>

2,添加服务、中间件

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 Microsoft.OpenApi.Models;
using System.Reflection;
using System.IO;


namespace swaggerdemo
{
    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文档
            services.AddSwaggerGen(c =>
            {
                c.ResolveConflictingActions(api=>api.Last());//同样的接口名 传递了不同参数,使用第一个api名称
                c.SwaggerDoc("v1", new OpenApiInfo()
                {//添加描述信息
                    Version = "v1",//描述文档的版本
                    Title = "openapi",//描述文档的标题
                    Description = "这个一个对外的文档",//描述信息
                    TermsOfService = new Uri("https://www.baidu.com"),//服务条款
                    Contact = new OpenApiContact()//联系方式
                    {
                        Name = "张迪",
                        Email = "123@qq.com",
                        Url = new Uri("https://www.zhangdi.com")
                    },
                    License = new OpenApiLicense()//许可证
                    {
                        Name = "Use under LICX",
                        Url = new Uri("https://example.com/license")
                    }
                });
                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作为JSON端点提供
            app.UseSwagger();
            //启用中间件来服务Swagger -ui (HTML, JS, CSS等),指定Swagger JSON端点。
            app.UseSwaggerUI(options =>
            {
                options.SwaggerEndpoint("/swagger/v1/swagger.json", "版本1");
                options.RoutePrefix = string.Empty;//要在应用程序的根目录(http://localhost:<port>/)提供Swagger UI
            });

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

 

posted on 2021-01-05 11:13  *Hunter  阅读(102)  评论(0编辑  收藏  举报

导航

AmazingCounters.com