资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C#(.NetCoreWebAPI)之API文档的生成(Swagger)

一 : 安装Swagger

Swashbuckle.AspNetCore
在NuGet 中,安装 Swashbuckle.AspNetCore :
C#(.Net Core WebAPI)之API文档的生成(Swagger)
我使用的版本为 : 5.0.0-rc2

创新互联专业为企业提供南和网站建设、南和做网站、南和网站设计、南和网站制作等企业网站建设、网页设计与制作、南和企业网站模板建站服务,十余年南和做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

二 : 引入Swagger功能

Ⅰ : Startup.cs
① ,ConfigureServices方法中:

public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2).AddJsonOptions(options =>
            {
                options.SerializerSettings.Formatting = Formatting.Indented;
            });
            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo()
                {
                    Title = "Swagger Test UI",
                    Version = "v1",
                    Description = "Aonaufly first ASP.NET Core Web API"
                });
                options.CustomSchemaIds(type => type.FullName); // 解决相同类名会报错的问题
                options.IncludeXmlComments(Path.Combine(Directory.GetCurrentDirectory(), "WebAPIPoco.xml")); // 标注要使用的 XML 文档
                options.DescribeAllEnumsAsStrings();
            });
        }

②:Configure中

        // 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
            {
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            //设置全局跨域
            app.UseCors(builder => builder.AllowAnyOrigin());
            app.UseHttpsRedirection();
            app.UseSwagger(c => { c.RouteTemplate = "swagger/{documentName}/swagger.json"; });
            // 在这里面可以注入
            app.UseSwaggerUI(options =>
            {
                options.ShowExtensions();
                options.ValidatorUrl(null);
                options.SwaggerEndpoint("/swagger/v1/swagger.json", "Aonaufly API V1");
                options.DocExpansion(DocExpansion.None);
            });
            app.UseMvc();
        }

三 :配置设置

①,到处项目XML , 加入1591禁止警告
C#(.Net Core WebAPI)之API文档的生成(Swagger)
②,将项目XML生成路径复制到项目根路径
copy $(TargetDir)WebAPIPoco.xml $(ProjectDir)WebAPIPoco.xml
C#(.Net Core WebAPI)之API文档的生成(Swagger)
③,重置默认网页为swagger , 默认是 api/values
C#(.Net Core WebAPI)之API文档的生成(Swagger)

四 :初始结果

C#(.Net Core WebAPI)之API文档的生成(Swagger)

五 : 测试

/// 
        /// 带参数的get请求
        /// 
        /// 
        /// 
        /// 输入 : int
        /// 输出 : string
        /// 
        /// 
        /// ID号
        /// String 
        /// 返回字符串
        /// 如果id为空
        // GET api/values/5
        [HttpGet("{id}")]
        [ProducesResponseType(201)]
        [ProducesResponseType(400)]
        public ActionResult Get(int id)
        {
            return "value";
        }

结果:
C#(.Net Core WebAPI)之API文档的生成(Swagger)


分享名称:C#(.NetCoreWebAPI)之API文档的生成(Swagger)
链接地址:http://cdkjz.cn/article/pogppp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220