配置Swagger

169 阅读1分钟

配置Swagger (Old Version)

使用Nuget Package Swashbuckle.AspNetCore 1.1.0

https://localhost:1111/swagger/v2/swagger.json

UI: https://localhost:1111/swagger/index.html

using Swashbuckle.AspNetCore.Swagger;

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().AddXmlSerializerFormatters();            

            services.AddSwaggerGen(options =>
            {
                options.IncludeXmlComments("../docs/BooksServiceSample.xml");
                options.SwaggerDoc("v2", new Info
                {
                    Title = "Books Service API",
                    Version = "v2",
                    Description = "Sample service for Professional C# 7",
                    Contact = new Contact { Name = "Christian Nagel", Url = "https://csharp.christiannagel.com" },
                    License = new License { Name = "MIT License" }
                });
            });
        }
        public async void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseMvc();
            
            app.UseSwagger();
            app.UseSwaggerUI(options =>
                options.SwaggerEndpoint("/swagger/v2/swagger.json", "Book Chapter Services"));
        }

配置Swagger (New Version)

使用Nuget Package Swashbuckle.AspNetCore 6.6.2

using Swashbuckle.AspNetCore.Swagger;

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().AddXmlSerializerFormatters();            

            services.AddSwaggerGen(options =>
            {
                 options.SwaggerDoc("v2", new OpenApiInfo
                 {
                     Title = "API",
                     Version = "v2",
                     Description = "Web API",
                     Contact = new OpenApiContact { Name = "Wa" },       
                 });
                    
                 // Set the comments path for the Swagger JSON and UI.
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                options.IncludeXmlComments(xmlPath);
            });
        }

        public async void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseMvc();
            app.UseSwagger();
            app.UseSwaggerUI(options =>
                options.SwaggerEndpoint("/swagger/v2/swagger.json", "Book Chapter Services"));
        }