将MiniProfiler连接到ASP.NET Core Web API Swagger

问题描述 投票:8回答:1

我发现只有this manual描述了如何使MiniProfiler与ASP.NET Web API和Swagger UI一起工作,但我没有找到任何描述如何使用MacroProfiler使ASP.NET Core Web API与Swagger UI显示结果的手册。

c# asp.net-core swagger-ui asp.net-core-webapi mvc-mini-profiler
1个回答
8
投票

您所需要的只是定制Swagger index.html文件,就像在documentation中解释的那样。创建自定义HTML文件后,在其中添加以下行:

<script async="async" id="mini-profiler" src="/profiler/includes.js?v=4.0.0.0" data-version="4.0.0.0" data-path="/profiler/" data-current-id="865f1487-f416-4d39-87fe-723e34847577" data-ids="" data-position="left" data-authorized="true" data-max-traces="15" data-toggle-shortcut="Alt+P" data-trivial-milliseconds="2.0" data-ignored-duplicate-execute-types="Open,OpenAsync,Close,CloseAsync"></script>

基本上上面的脚本是MiniProfiler.Current.RenderIncludes()方法的输出。

下面是ConfigureServicesConfigure方法,以了解如何配置Swagger和Miniprofiler

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    // Register the Swagger generator, defining one or more Swagger documents
    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
    });

    services.AddMiniProfiler(options => 
        options.RouteBasePath = "/profiler"
    );
}

// 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();
        app.UseMiniProfiler();
    }

    app.UseSwagger();
    app.UseSwaggerUI(c => {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
        c.IndexStream = () => GetType().GetTypeInfo().Assembly.GetManifestResourceStream("SOMpSwaggerNetCore.SwaggerIndex.html");
    });
    app.UseMvc();
}
© www.soinside.com 2019 - 2024. All rights reserved.