netcore 添加swagger

时间:2019-10-28
本文章向大家介绍netcore 添加swagger,主要包括netcore 添加swagger使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.添加相应的nuget包

 2.配置服务和swaggerui

startup.cs 中

configureServices 中添加下面代码:

//swagger
            services.AddSwaggerGen();
            services.ConfigureSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new Info
                {
                    Version = "v1",
                    Title = "Ucent API"
                });

                options.IncludeXmlComments(Path.Combine("./Ucent.xml"));
                options.DescribeAllEnumsAsStrings();
   
            });

Configure 中添加:

            //swagger
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                string swaggerJsonBasePath = string.IsNullOrWhiteSpace(c.RoutePrefix) ? "." : "..";
                c.SwaggerEndpoint($"{swaggerJsonBasePath}/swagger/v1/swagger.json", "My API");
            });

3.修改输出目录

 注意:路径跟ConfigureServices中配置的路径对用

4.修改lanchSettings.json

 修改启动路径

原文地址:https://www.cnblogs.com/leolzi/p/11752052.html