swagger UI
第一步:首先添加包Swashbuckle.AspNetCore (可通過微軟添加包命令Install-Package 包名進行添加,也可以通過管理NuGet程序包進行添加)
第二步:修改launchUrl為swagger,即程序啟動后進入swagger UI風格頁面也可以說Rest風格。
{ "$schema": "http://json.schemastore.org/launchsettings.json", "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:55360", "sslPort": 44347 } }, "profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "launchUrl": "swagger", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "WebApi": { "commandName": "Project", "launchBrowser": true, "launchUrl": "swagger", "applicationUrl": "https://localhost:5001;http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } }
第三步:在Startup.cs中的方法ConfigureServices方法中添加swagger 相關代碼。
public voidConfigureServices(IServiceCollection services) { services.AddMvc(); #region 添加SwaggerUI services.AddSwaggerGen(options => { options.SwaggerDoc("v1", new Info { Title = "dapper API接口文檔", Version = "v1", Description = "RESTful API for Dapper", TermsOfService = "Z Terms Of Service", Contact = new Contact { Name = "zhang", Email = "904086892@qq.com", Url = "" } }); }); #endregion }
第四步:在Startup.cs中的方法Configure方法中添加swagger 相關代碼。
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) app.UseDeveloperExceptionPage(); else app.UseHsts(); #region 使用SwaggerUI app.UseSwagger(); app.UseSwaggerUI(options => { options.SwaggerEndpoint("/swagger/v1/swagger.json", "Dapper API V1"); }); #endregion app.UseHttpsRedirection(); app.UseMvc(); }
最后運行項目,可在該頁面下進行使用接口。效果如下:
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。