.net core webapi 中使用Swagger


1.添加Swashbuckle.AspNetCore安裝包

方式1:使用程序包管理器控制台,安裝命令:Install-Package Swashbuckle.AspNetCore 

方式2:在Nuget包管理器:輸入Swashbuckle.AspNetCore,下載安裝。如下圖:

2.在Startup 文件中添加配置:

  // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddSwaggerGen(c =>
                                   {
                                       c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
                                   });

        }

  

 public void Configure(IApplicationBuilder app, IHostingEnvironment 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");
                             });

            app.UseMvc();
        }

3.輸入地址訪問http://localhost:52408/swagger/訪問,效果如下圖:

 


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com