1. 程式人生 > >VS2017 安裝Swagger初步認識

VS2017 安裝Swagger初步認識

技術 r.js 註冊 line developer lan page ces esc

1.安裝NuGet包

2.配置

3.運行測試

參考博客:https://www.cnblogs.com/yilezhu/p/9241261.html

一 安裝NuGet包

包名:Swashbuckle.AspNetCore

技術分享圖片

二 配置

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Swashbuckle.AspNetCore.Swagger; namespace WebApplicationCoreAPIStudy4 {
public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); ////註冊Swagger生成器,定義一個和多個Swagger 文檔 //services.AddSwaggerGen(c => //{ // c.SwaggerDoc("v1", new Info { Title = "My API_info", Version = "v1_info" }); //}); //註冊Swagger生成器,定義一個和多個Swagger 文檔 services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Version = "v1", Title = "yilezhu‘s API", Description = "A simple example ASP.NET Core Web API", TermsOfService = "None", Contact = new Contact { Name = "依樂祝", Email = string.Empty, Url = "http://www.cnblogs.com/yilezhu/" }, License = new License { Name = "許可證名字", Url = "http://www.cnblogs.com/yilezhu/" } }); // 為 Swagger JSON and UI設置xml文檔註釋路徑 var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);//獲取應用程序所在目錄(絕對,不受工作目錄影響,建議采用此方法獲取路徑) var xmlPath = Path.Combine(basePath, "WebApplicationCoreAPIStudy4.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) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseHsts(); } app.UseHttpsRedirection(); app.UseMvc(); //啟用中間件服務生成Swagger作為JSON終結點 app.UseSwagger(); //啟用中間件服務對swagger-ui,指定Swagger JSON終結點 app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1_endpoint"); //加上後,訪問地址:https://localhost:44389 //c.RoutePrefix = string.Empty;//訪問地址:https://localhost:44389/swagger }); } } }

技術分享圖片

三 運行測試

技術分享圖片

技術分享圖片

VS2017 安裝Swagger初步認識