12

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.

Vadim Ovchinnikov
  • 13,327
  • 5
  • 62
  • 90

1 Answers1

12

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();
}
Alexey Andrushkevich
  • 5,992
  • 2
  • 34
  • 49
  • 1
    OK, I've already done this but customization HTML is the challenge because I can't specify razor statements there to render MiniProfiler view. – Vadim Ovchinnikov Mar 07 '18 at 16:34
  • You don't need to. I have added the exact script which shall be added into the HTML file. The solution is not ideal because if you later update the version of the MiniProfiler the output of `RenderIncludes()` method might change and this solution will stop working. – Alexey Andrushkevich Mar 07 '18 at 16:38
  • If you want to render MiniProfiler includes on the fly you can add a bit more JS code to this HTML file which basically makes an AJAX request to your API which returns the result of `MiniProfiler.Current.RenderIncludes` method – Alexey Andrushkevich Mar 07 '18 at 16:41
  • 1
    If you didn't set the `RouteBasePath` property in MiniProfiler's options as it's shown above then use default path `/mini-profiler-resources/includes.js` – Alexey Andrushkevich Mar 07 '18 at 16:49
  • I sometimes get 404 for `/mini-profiler-resources/results` so I don't get profiler results for query (it happens occasionally, no pattern detected) but after trying the same it's OK. Is it MiniProfiler bug? – Vadim Ovchinnikov Mar 08 '18 at 08:39
  • I guess so, the MiniProfiler for ASP.NET Core is the pre-release package. I would report the issue on their GitHub. – Alexey Andrushkevich Mar 08 '18 at 08:49