WebApi框架搭建(一)集成Swagger

2021-03-13 12:32

阅读:678

标签:deb   res   过程   run   col   called   build   star   注意   

概要:

  1、安装包

  2、插入代码:Startup.cs

  3、创建项目XML注释文档

  4、配置默认启动打开Swagger

 

 

过程:

  1、安装包

    Swashbuckle.AspNetCore

  2、插入代码:Startup.cs

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            // 注册Swagger服务,声明一个或多个文档
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("V1", new OpenApiInfo { Title = "HYFWAPI", Version = "V1" });
                c.IncludeXmlComments(string.Format("{0}/bin/WebApi.xml", AppDomain.CurrentDomain.BaseDirectory));
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            // Enable middleware to serve generated Swagger as a JSON endpoint.
            app.UseSwagger();

            // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
            // specifying the Swagger JSON endpoint.
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/V1/swagger.json", "My API V1");

                //要在应用的根(http://localhost:/) 处提供 Swagger UI,请将 RoutePrefix 属性设置为空字符串:
                //c.RoutePrefix = string.Empty;
            });

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }        

    注意:c.SwaggerDoc("V1" 与 c.SwaggerEndpoint("/swagger/V1/swagger.json"中的"V1"要一致

  3、创建项目XML注释文档

    右键项目→属性→生成→选中下方的 "XML文档文件" ,"bin\Debug\netcoreapp3.1\bin\WebApi.xml"

    取消显示错误警告:1591

  4、配置默认启动打开Swagger

    修改launchSettings.json中的"launchUrl": "swagger"

 

WebApi框架搭建(一)集成Swagger

标签:deb   res   过程   run   col   called   build   star   注意   

原文地址:https://www.cnblogs.com/mellen/p/12546141.html


评论


亲,登录后才可以留言!