引包
代码
public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); //help services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Version = "v1", Title = "接口文档", Description = "RESTful API for Hicard", TermsOfService = "None", Contact = new Contact { Name = "-", Email = "-", Url = "" } }); //Set the comments path for the swagger json and ui. c.IncludeXmlComments(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Doc.xml")); //文档路径 // c.OperationFilter<HttpHeaderOperation>(); // 添加httpHeader参数 }); }
public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFactory logFac) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "v1"); }); }
文档生成需要在项目属性里设置
好了。运行项目,请求 http://127.0.0.1/swagger/index.html 看效果