码迷,mamicode.com
首页 > Windows程序 > 详细

WebApi框架搭建(一)集成Swagger

时间:2020-03-22 15:53:45      阅读:106      评论:0      收藏:0      [点我收藏+]

标签: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:<port>/) 处提供 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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!