ASP.NET Core WebApi使用Swagger

先在項目中引用Nuget包json

Install-Package Swashbuckle.AspNetCore

 

安裝以後須要在Startup.cs文件加入以下代碼app

        public void ConfigureServices(IServiceCollection services)
        {
            //...
            services.AddSwaggerGen(c =>
                       {
                           c.SwaggerDoc("v1", new Info { Title = "Demo", Version = "v1" });
                           var xmlFile = $"{Assembly.GetEntryAssembly().GetName().Name}.xml";
                           var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                           c.IncludeXmlComments(xmlPath);
                       });
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            //...
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "Demo API");
            });
        }

 

最後在項目屬性->生成->輸出中勾選XML文檔文件ui

 

運行項目時可在項目端口後加上/swagger就能夠查看文檔頁面了,以下spa

相關文章
相關標籤/搜索