zoukankan      html  css  js  c++  java
  • .net core中使用swagger 配置Startup.cs

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Logging;
    using Microsoft.Extensions.Options;
    using Swashbuckle.AspNetCore.Swagger;
    namespace WebApi
    {
    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);
    #region Swagger
    services.AddSwaggerGen(c =>
    {
    c.SwaggerDoc("v1", new Info
    {
    Version = "v0.1.0",
    Title = ".NET Core API",
    Description = "框架说明文档",
    TermsOfService = "None",
    Contact = new Swashbuckle.AspNetCore.Swagger.Contact { Name = "Blog.Core", Email = "1084013577@QQ.com", Url = "https://www.jianshu.com/u/94102b59cc2a" }
    });
    //配置注释
    var basePath = Microsoft.DotNet.PlatformAbstractions.ApplicationEnvironment.ApplicationBasePath;
    var xmlPath = Path.Combine(basePath, "WebApi.xml");//这个就是刚刚配置的xml文件名
    c.IncludeXmlComments(xmlPath, true);//默认的第二个参数是false,这个是controller的注释,记得修改

    var xmlModelPath = Path.Combine(basePath, "Model.xml");//这个就是Model层的xml文件名
    c.IncludeXmlComments(xmlModelPath);
    });

    #endregion
    }

    // 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();
    }
    #region Swagger
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "ApiHelp V1");
    c.RoutePrefix = "";//路径配置,设置为空,表示直接访问该文件, 把launchSettings.json 的 launchUrl 给去掉,开发和生产环境都会以swagger 为首页
    });
    #endregion
    app.UseMvc();
    }
    }
    }

  • 相关阅读:
    【Quartz】Quartz将Job保存在数据库中所需表的说明
    第十章 springboot + logback
    第二章 rabbitmq在mac上的安装
    第九章 springboot + mybatis + 多数据源 (AOP实现)
    第一章 AOP
    第八章 springboot + mybatis + 多数据源
    第三章 线程安全的DateFormat工具类
    第六章 consul UI
    第五章 consul key/value
    附1 consul常用命令+常用选项
  • 原文地址:https://www.cnblogs.com/yjm8023/p/12671672.html
Copyright © 2011-2022 走看看