Swaggerui 可以为我们的webapi提供美观的在线文档,如下图:

swaggerui在asp.net web api core 中的应用

 实现步骤:

  • NuGet Packages  Install-Package Swashbuckle.AspNetCore
  • 在startup文件中配置swagger
                // Register the Swagger generator, defining one or more Swagger documents
                services.AddSwaggerGen(c =>
                {
                    c.SwaggerDoc("v1", new Info
                    {
                        Version = "v1",
                        Title = "ToDo API",
                        Description = "A simple example ASP.NET Core Web API",
                        TermsOfService = "None",
                        Contact = new Contact { Name = "Shayne Boyer", Email = "", Url = "https://twitter.com/spboyer" },
                        License = new License { Name = "Use under LICX", Url = "https://example.com/license" }
                    });
    
                    //Set the comments path for the swagger json and ui.
                    var basePath = PlatformServices.Default.Application.ApplicationBasePath;
                    var xmlPath = Path.Combine(basePath, "MyWebApiCore.xml");
                    c.IncludeXmlComments(xmlPath);
                });
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
            {
                loggerFactory.AddConsole(Configuration.GetSection("Logging"));
                loggerFactory.AddDebug();
    
                app.UseMvc();
                app.UseSwagger();
                app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
                });
            }
    View Code

相关文章:

  • 2021-05-25
  • 2021-06-12
  • 2019-07-22
  • 2019-07-04
  • 2022-12-23
猜你喜欢
  • 2022-03-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-24
相关资源
相似解决方案