Wire up MiniProfiler to ASP.NET Core Web API Swagg

2019-03-29 05:32发布

I found only this manual describing how to make MiniProfiler work with ASP.NET Web API and Swagger UI, but I didn't find any manual describing how to make ASP.NET Core Web API work with MiniProfiler to show results in Swagger UI.

1条回答
Root(大扎)
2楼-- · 2019-03-29 05:52

All you need is to customize Swagger index.html file just like it's explained in the documentation. After you created a custom HTML file add the following line into it:

<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>

Basically the script above is the output of the MiniProfiler.Current.RenderIncludes() method.

Here is below the ConfigureServices and the Configure methods to see how both Swagger and Miniprofiler are configured

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();
}
查看更多
登录 后发表回答