.Net Core WebApi项目实现Swagger和MiniProfiler集成

本文介绍了如何在.NET Core WebApi项目中集成Swagger和MiniProfiler,以实现接口性能分析和测试。首先,通过安装Nuget包引入MiniProfiler。接着,确保其在`UseEndpoints`之前注册并启用。然后,自定义Swagger首页`index.html`,并将MiniProfiler的Tag标签插入其中。当遇到MiniProfiler的Tag为空时,检查注册顺序。最后,展示了如何在代码中使用MiniProfiler进行性能分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景

在开发一个webapi项目,使用了Swagger首页展示Http接口。为了测试、提升接口性能,现在对接口加入分析工具。

MiniProfiler是一款很好的轻量级性能分析工具,提供库和UI。通过它可以查看程序的时间消耗,提供程序调试和性能优化帮助。

MiniProfiler is a library and UI for profiling your application. By letting you see where your time is spent, which queries are run, and any other custom timings you want to add, MiniProfiler helps you debug issues and optimize performance.

官网 https://miniprofiler.com/dotnet/

 

1、安装Nuget包

Install-Package MiniProfiler.AspNetCore.Mvc

2、注册和启用MiniProfiler

   public void ConfigureServices(IServiceCollection services)
        {
            /*
             * 其他代码
             */
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v1.0",
                    Title = "GDWC.Core.API",
                    Description = "说明文档",
                    TermsOfService = null,
                    Contact = new OpenApiContact
                    {
                        Name = "GDWC.Core",
                        Email = "",
                        Url = new Uri("http://www.whfhjc.com.cn/"),
                    }
                });
        
            services.AddMiniProfiler(option =>
            {
                option.RouteBasePath = "/profiler";
                //(option.Storage as MemoryCacheStorage).CacheDuration = TimeSpan.FromMinutes(10);
            });
            /*
             * 其他代码
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值