标签:agg 部分 except nts 自动 dev mic ocs call
Swagger工具为你的Asp Net Core生成漂亮的API文档。目前社会上大部分都是一个服务端为N个客户端提供接口,往往我们需要提供一个友好的API文档,Swagger的出现,几乎使得API文档完全自动化。
public Startup(IConfiguration configuration, IHostingEnvironment env) { Configuration = configuration; this.Env = env; } public IHostingEnvironment Env { get; } public IConfiguration Configuration { get; } public string[] docs = new[] { "未分类" }; // 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_2); if (Env.IsDevelopment()) { services.AddSwaggerGen(options => { foreach (var d in docs) options.SwaggerDoc(d, new Info { Version = d }); options.DocInclusionPredicate((docName, description) => { description.TryGetMethodInfo(out MethodInfo mi); var attr = mi.DeclaringType.GetCustomAttribute<ApiExplorerSettingsAttribute>(); if (attr != null) { return attr.GroupName == docName; } else { return docName == "未分类"; } }); var ss = options.SwaggerGeneratorOptions; options.IncludeXmlComments(@"E:\VsCode\C#\.Net Core\第三章 利用Swagger生成接口文档\SwaggerDemo\bin\Debug\netcoreapp2.2\SwaggerDemo.xml"); }); } } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseMvc(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseSwagger() .UseSwaggerUI(options => { options.DocumentTitle = "Ron.liang Swagger 测试文档"; foreach (var item in docs) options.SwaggerEndpoint($"/swagger/{item}/swagger.json", item); }); } }
代码中涉及到的xml需要换成自己的路径
为保证自己项目xml文档的生成 把.csproj文件添加以下属性
doenet build--->dotnet run-->访问https://localhost:8090/swagger/index.html(此处需要换成自己的网站地址)可以看到api文档已经成功生成,赶快自己点击玩玩把~~~
上面我们用的默认API文档,实际工作中我们需要分类以及注释信息等。下面我们添加一个测试分组
再次访问
标签:agg 部分 except nts 自动 dev mic ocs call
原文地址:https://www.cnblogs.com/FashionDoo/p/10525448.html