.Net Core Config Swagger

首先安装NuGet包 Swashbuckle.AspNetCore

紧接着 Startup 的代码

using Swashbuckle.AspNetCore.SwaggerUI;//要添加的命名空间

namespace Project.API
{
    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.AddControllers();

            services.AddSwaggerGen(config =>
            {
                config.SwaggerDoc("v1", new OpenApiInfo() { Title = "项目名称 Api", Version = "v1" });//项目名称填你的项目名称,下同
                config.CustomSchemaIds(type => type.FullName);
                config.IncludeXmlComments($"{AppDomain.CurrentDomain.BaseDirectory}/Project.API.xml");//Project.API 为你的项目名称
            });
            Register(services);

            services.AddOptions();


        }

        /// <summary>
        /// 依赖注入
        /// </summary>
        /// <param name="services"></param>
        private static void Register(IServiceCollection services)
        {


        }

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

            app.UseSwagger(c =>
            {
                c.RouteTemplate = "api-doc/{documentName}/swagger.json";//配置后,你的最终访问路径就就是 /api-doc/index.html
            });
            app.UseSwaggerUI(c =>
            {
                c.InjectJavascript("/zh_CN.js");//中文包,怎么测试也出不来,等待一个大佬解决下,所以这里就不放中文包了,因为根本用不了,(这句可以删
                c.ShowExtensions();
                c.DocExpansion(DocExpansion.None);
                c.RoutePrefix = "api-doc";
                c.SwaggerEndpoint("v1/swagger.json", "项目名称 Api v1");
            });

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute("default", "{controller=ApiHome}/{action=Index}/{id?}");
            });
        }
    }
}

最后两步
你的项目-->右键属性-->生成事件-->生成后事件命令行--> copy $(TargetDir){项目名称}.xml $(ProjectDir){项目名称}.xml

你的项目-->右键属性-->生成事件-->生成后事件命令行--> 输出 如图所示

最后运行试试

记得切换端口号 https://localhost:{xxxx}/api-doc/index.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值