Skip to content

Commit

Permalink
Use Razor Pages route value in MiniProfiler name (#495)
Browse files Browse the repository at this point in the history
If you are using Razor Pages instead of controllers and views MiniProfiler shows all result names in the results list as `/`.  This is because `routeData.Values["controller"]` and `routeData.Values["action"]` are null.  

This change checks if `routeData.Values["pages"]` has a value and uses that for the MiniProfiler name if available.

I've also added a Razor Page to the Asp.Net Core 3 sample for testing
  • Loading branch information
jeffpapp authored Jun 8, 2020
1 parent 3c467c9 commit a0518a5
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 2 deletions.
37 changes: 37 additions & 0 deletions samples/Samples.AspNetCore3/Pages/RazorPagesSample.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
@page

@{
ViewData["Title"] = "Razor Pages Sample";
}
<div class="col-md-12">
<div class="page-header">
<h2>ASP.NET Core 3: Behold MiniProfiler in the top right (Razor Pages Version)!</h2>
</div>
</div>
<div class="row">
<partial name="Index.LeftPanel" />
<partial name="Index.RightPanel" />
</div>
@section scripts {
<script>
$(function () {
// these links should fire ajax requests, not do navigation
$('.ajax-requests a').click(function () {
var $clicked = $(this),
$spinner = $('<span class="glyphicon glyphicon-refresh spinning" title="Working..."></span>').appendTo($clicked.parent()),
$results = $('.ajax-results');
$.ajax({
type: 'GET',
url: this.href,
success: function (data) {
$('<p class="ajax-result">').append(data).appendTo($results);
},
error: function () { $results.append('<p>ERROR!</p>'); },
complete: function () { $spinner.remove(); }
});
return false;
});
});
</script>
}
3 changes: 3 additions & 0 deletions samples/Samples.AspNetCore3/Pages/_ViewImports.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@using Samples.AspNetCore
@namespace Samples.AspNetCore.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
3 changes: 3 additions & 0 deletions samples/Samples.AspNetCore3/Pages/_ViewStart.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@{
Layout = "_Layout";
}
7 changes: 6 additions & 1 deletion samples/Samples.AspNetCore3/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Samples.AspNetCore
public class Startup
{
public static string SqliteConnectionString { get; } = "Data Source=Samples; Mode=Memory; Cache=Shared";

private static readonly SqliteConnection TrapConnection = new SqliteConnection(SqliteConnectionString);

public Startup(IWebHostEnvironment env)
Expand Down Expand Up @@ -118,7 +119,11 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseMiniProfiler()
.UseStaticFiles()
.UseRouting()
.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute());
.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
endpoints.MapRazorPages();
});

var serviceScopeFactory = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>();
using (var serviceScope = serviceScopeFactory.CreateScope())
Expand Down
1 change: 1 addition & 0 deletions samples/Samples.AspNetCore3/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a asp-area="" asp-controller="Home" asp-action="Index">Home</a></li>
<li><a href="/RazorPagesSample">Razor Pages</a></li>
</ul>
</div>
</div>
Expand Down
6 changes: 5 additions & 1 deletion src/MiniProfiler.AspNetCore/MiniProfilerMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,14 @@ private void EnsureName(MiniProfiler profiler, HttpContext context)
.ToStringRecycle();

var routeData = context.GetRouteData();
if (routeData != null)
if (routeData?.Values["controller"] != null)
{
profiler.Name = routeData.Values["controller"] + "/" + routeData.Values["action"];
}
else if (routeData?.Values["page"] != null)
{
profiler.Name = routeData.Values["page"].ToString();
}
else
{
profiler.Name = url;
Expand Down

0 comments on commit a0518a5

Please sign in to comment.