.netcore 3.1 Swagger

1.新建netcore web api项目

在这里插入图片描述
在这里插入图片描述

2.安装NuGet包 Swashbuckle.AspNetCore(最新稳定版)

在这里插入图片描述

3.在startup.cs添加服务

在ConfigureServices方法里用AddSwaggerGen添加服务
在Configure里加上UseSwagger和UseSwaggerUI

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }
        
        readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";//名字随便起

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("V1", new OpenApiInfo
                {
                    Version = "V1",
                    Title = "Epitomize.WebApi",
                    Description = "项目框架Api服务"
                });
                //获取xml文件名
                string xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                //获取xml文件路径
                string xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                //此路径要与项目属性中 设置的输出xml路径保持一致
                //是否显示注释
                c.IncludeXmlComments(xmlPath);
            });
            services.AddControllers();
        }

        // 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();

                app.UseSwagger();
                app.UseSwaggerUI(c =>
                {
                    //标题
                    c.DocumentTitle = "Epitomize.WebApi";
                    c.SwaggerEndpoint("Swagger/V1/Swagger.json", "Epitomize.WebApi");
                    //默认启动swagger页面
                    c.RoutePrefix = string.Empty;
                });
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // 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.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

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

4.添加xml文件生成路径

打开项目属性,在输出=>xml文档文件位置写上相对路径(此路径和代码c.IncludeXmlComments(xmlPath)里的xmlPath要保持一致)
在这里插入图片描述

5.可以在里输入示例的json,也会显示在swagger ui中

在这里插入图片描述

原文地址:
[1]: https://blog.csdn.net/m0_38110784/article/details/106984521

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值