zoukankan      html  css  js  c++  java
  • NetCoreAPI添加Swagger

    public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
                services.AddOptions();
                services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
                services.AddHostedService<TaskService>();
           //添加Swagger
                services.AddSwaggerGen(c =>
                {
                    c.SwaggerDoc("v1", new  Swashbuckle.AspNetCore.Swagger.Info
                    {
                        Version = "v1",
                        Title = "WebAPI",
                        //Description = "一个简单的ASP.NET Core Web API",
                        //TermsOfService = new Uri("https://www.cnblogs.com/taotaozhuanyong"),
                        //Contact = new OpenApiContact
                        //{
                        //    Name = "bingle",
                        //    Email = string.Empty,
                        //    Url = new Uri("https://www.cnblogs.com/taotaozhuanyong"),
                        //},
                        //License = new OpenApiLicense
                        //{
                        //    Name = "许可证",
                        //    Url = new Uri("https://www.cnblogs.com/taotaozhuanyong"),
                        //}
                    });
    
                    //为 Swagger JSON and UI设置xml文档注释路径
                    var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                    var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                    c.IncludeXmlComments(xmlPath);
                });
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseHsts();
                }
                app.UseHttpsRedirection();
                app.UseDefaultFiles();
                app.UseMvc();
                app.UseStaticFiles();
    
                //启用中间件服务生成Swagger作为JSON终结点
                app.UseSwagger();
                //启用中间件服务对swagger-ui,指定Swagger JSON终结点
                app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
                    c.RoutePrefix = string.Empty;
                });
            }
        }
  • 相关阅读:
    完美世界(完美世界(北京)网络技术有限公司)
    盛大
    虚商来袭,蜗牛免卡动真格了!--------------------------数据控?看看虚拟运营有多热
    使用properties配置文件为javabean注入属性值
    spring的list ,set,map,properties注入(set,get注入)
    控制反转和依赖注入
    bean的生命周期以及延迟实例化
    bean的单例
    spring的配置文件和加载
    mysql外键设置选项
  • 原文地址:https://www.cnblogs.com/nnnnnn/p/12058484.html
Copyright © 2011-2022 走看看