netcore3.0 webapi集成Swagger 5.0,Swagger使用
2021-01-29 13:15
标签:only bho returns star ima generic asp poi png Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。 作用: 1.接口的文档在线自动生成。 2.功能测试 本文转自:https://www.cnblogs.com/datacool/p/11805585.html 今天来尝尝鲜。貌似.net core 3.0使用Swagger 4.0.1会报错,随手一搜,还没人写这个把调试通过的代码贴一下: 依赖包: 控制器: netcore3.0 webapi集成Swagger 5.0,Swagger使用 标签:only bho returns star ima generic asp poi png 原文地址:https://www.cnblogs.com/bgh408/p/11849845.htmlSwagger使用
1、描述
1 using Microsoft.AspNetCore.Builder;
2 using Microsoft.AspNetCore.Hosting;
3 using Microsoft.Extensions.Configuration;
4 using Microsoft.Extensions.DependencyInjection;
5 using Microsoft.Extensions.Hosting;
6 using Microsoft.OpenApi.Models;
7 using System;
8 using System.IO;
9 using System.Reflection;
10
11 namespace WebApplication4
12 {
13 public class Startup
14 {
15 public Startup(IConfiguration configuration)
16 {
17 Configuration = configuration;
18 }
19
20 public IConfiguration Configuration { get; }
21
22 // This method gets called by the runtime. Use this method to add services to the container.
23 public void ConfigureServices(IServiceCollection services)
24 {
25 services.AddControllers();
26 services.AddSwaggerGen(c =>
27 {
28 c.SwaggerDoc("v1", new OpenApiInfo { Title = ".Net Core中间件API文档", Version = "v1" });
29 // 为 Swagger 设置xml文档注释路径
30 var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
31 var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
32 c.IncludeXmlComments(xmlPath);
33 });
34 }
35
36 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
37 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
38 {
39 //启用中间件服务生成Swagger
40 app.UseSwagger();
41 //启用中间件服务生成SwaggerUI,指定Swagger JSON终结点
42 app.UseSwaggerUI(c =>
43 {
44 c.SwaggerEndpoint("/swagger/v1/swagger.json", ".Net Core中间件API文档 V1");
45 c.RoutePrefix = string.Empty;//设置根节点访问
46 });
47 if (env.IsDevelopment())
48 {
49 app.UseDeveloperExceptionPage();
50 }
51 app.UseRouting();
52 app.UseAuthorization();
53 app.UseEndpoints(endpoints =>
54 {
55 endpoints.MapControllers();
56 });
57 }
58 }
59 }
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Threading.Tasks;
5 using Microsoft.AspNetCore.Mvc;
6 using Microsoft.Extensions.Logging;
7
8 namespace WebApplication4.Controllers
9 {
10 ///
文章标题:netcore3.0 webapi集成Swagger 5.0,Swagger使用
文章链接:http://soscw.com/index.php/essay/48698.html