.Net Core 添加Swagger步骤(Core 3.0配置步骤)

Startup.cs文件:

NuGet下载:Swashbuckle.AspNetCore

引入命名空间:

using Swashbuckle.AspNetCore.Swagger;

基本配置:

Startup.ConfigureServices方法添加:

//注册Swagger生成器,定义一个和多个Swagger 文档
services.AddSwaggerGen(c =>
{
     c.SwaggerDoc("v1", new Info { Title = "Vedm API V1", Version = "v1" });
});

Startup.Configure方法添加:

//启用中间件服务生成Swagger作为JSON终结点
app.UseSwagger();
//启用中间件服务对swagger-ui,指定Swagger JSON终结点
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "Vedm API V1");
});

启用XML注释:

右键单击“解决方案资源管理器”中的项目,然后选择“属性”

查看“生成”选项卡的“输出”部分下的“XML 文档文件”框

.Net Core 添加Swagger步骤(Core 3.0配置步骤)_第1张图片

Startup.ConfigureServices方法添加:

 services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new Info
                {
                    Version = "v1",
                    Title = "Vedm API V1"
                });
                // 为 Swagger JSON and UI设置xml文档注释路径
                var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);//获取应用程序所在目录(绝对,不受工作目录影响,建议采用此方法获取路径)
                var xmlPath = Path.Combine(basePath, "Lsxx.Vedm.App.xml");
                options.IncludeXmlComments(xmlPath);
            });

 

 Net Core 3.0

1、NuGet下载:Swashbuckle.AspNetCore

.Net Core 添加Swagger步骤(Core 3.0配置步骤)_第2张图片

 

2、Startup.cs文件:

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();

            //添加Swagger
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v1",
                    Title = "Api Swagger",
                    Description = "基于.NET Core 3.0 的Api Swagger"
                });
                // 加载程序集的xml描述文档
                var baseDirectory = System.AppDomain.CurrentDomain.BaseDirectory;
                var xmlFile = System.AppDomain.CurrentDomain.FriendlyName + ".xml";
                var xmlPath = Path.Combine(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();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });


            app.UseSwagger(c =>
            {
                c.RouteTemplate = "api-docs/{documentName}/swagger.json";
            });
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/api-docs/v1/swagger.json", "Api v1");
            });
        }

3、xml文档

.Net Core 添加Swagger步骤(Core 3.0配置步骤)_第3张图片

.Net Core 添加Swagger步骤(Core 3.0配置步骤)_第4张图片

 

文章参考:

https://www.cnblogs.com/yilezhu/p/9241261.html

https://www.cnblogs.com/yanbigfeg/p/9232844.html

你可能感兴趣的:(.NET,Core)