/// <summary> /// Returns either json or full page html of a previous <see cref="MiniProfiler"/> session, /// identified by its <c>"?id=GUID"</c> on the query. /// </summary> /// <param name="context">The context to get a profiler response for.</param> private string GetSingleProfilerResult(HttpContext context) { Guid id; ResultRequest clientRequest = null; // When we're rendering as a button/popup in the corner, it's an AJAX/JSON request. // If that's absent, we're rendering results as a full page for sharing. var jsonRequest = context.Request.Headers["Accept"]?.Contains("application/json") == true; // Try to parse from the JSON payload first if (jsonRequest && context.Request.ContentLength > 0 && ResultRequest.TryParse(context.Request.InputStream, out clientRequest) && clientRequest.Id.HasValue) { id = clientRequest.Id.Value; } else if (Guid.TryParse(context.Request["id"], out id)) { // We got the guid from the querystring } else if (Options.StopwatchProvider != null) { // Fall back to the last result id = Options.Storage.List(1).FirstOrDefault(); } if (id == default(Guid)) { return(jsonRequest ? NotFound(context) : NotFound(context, "text/plain", "No Guid id specified on the query string")); } var profiler = Options.Storage.Load(id); string user = Options.UserIdProvider?.Invoke(context.Request); Options.Storage.SetViewed(user, id); if (profiler == null) { return(jsonRequest ? NotFound(context) : NotFound(context, "text/plain", "No MiniProfiler results found with Id=" + id.ToString())); } bool needsSave = false; if (profiler.ClientTimings == null && clientRequest?.TimingCount > 0) { profiler.ClientTimings = ClientTimings.FromRequest(clientRequest); needsSave = true; } if (!profiler.HasUserViewed) { profiler.HasUserViewed = true; needsSave = true; } if (needsSave) { Options.Storage.Save(profiler); } if (!AuthorizeRequest(context, isList: false, message: out string authorizeMessage)) { context.Response.ContentType = "application/json"; return(@"""hidden"""); // JSON } return(jsonRequest ? ResultsJson(context, profiler) : ResultsFullPage(context, profiler)); }
/// <summary> /// Returns either json or full page html of a previous <c>MiniProfiler</c> session, /// identified by its <c>"?id=GUID"</c> on the query. /// </summary> private static string GetSingleProfilerResult(HttpContext context) { // when we're rendering as a button/popup in the corner, we'll pass ?popup=1 // if it's absent, we're rendering results as a full page for sharing var isPopup = context.Request["popup"].HasValue(); // this guid is the MiniProfiler.Id property // if this guid is not supplied, the last set of results needs to be // displayed. The home page doesn't have profiling otherwise. Guid id; if (!Guid.TryParse(context.Request["id"], out id)) { id = MiniProfiler.Settings.Storage.List(1).FirstOrDefault(); } if (id == default(Guid)) { return(isPopup ? NotFound(context) : NotFound(context, "text/plain", "No Guid id specified on the query string")); } MiniProfiler.Settings.EnsureStorageStrategy(); var profiler = MiniProfiler.Settings.Storage.Load(id); var provider = WebRequestProfilerProvider.Settings.UserProvider; string user = null; if (provider != null) { user = provider.GetUser(context.Request); } MiniProfiler.Settings.Storage.SetViewed(user, id); if (profiler == null) { return(isPopup ? NotFound(context) : NotFound(context, "text/plain", "No MiniProfiler results found with Id=" + id.ToString())); } bool needsSave = false; if (profiler.ClientTimings == null) { profiler.ClientTimings = ClientTimings.FromRequest(context.Request); if (profiler.ClientTimings != null) { needsSave = true; } } if (!profiler.HasUserViewed) { profiler.HasUserViewed = true; needsSave = true; } if (needsSave) { MiniProfiler.Settings.Storage.Save(profiler); } var authorize = MiniProfiler.Settings.Results_Authorize; if (authorize != null && !authorize(context.Request)) { context.Response.ContentType = "application/json"; return("hidden".ToJson()); } return(isPopup ? ResultsJson(context, profiler) : ResultsFullPage(context, profiler)); }
/// <summary> /// Returns either JSON or full page HTML of a previous <c>MiniProfiler</c> session, /// identified by its <c>"?id=GUID"</c> on the query. /// </summary> /// <param name="context">The context to get a profiler response for.</param> private async Task <string> GetSingleProfilerResultAsync(HttpContext context) { Guid id; ResultRequest clientRequest = null; // When we're rendering as a button/popup in the corner, it's an AJAX/JSON request. // If that's absent, we're rendering results as a full page for sharing. bool jsonRequest = context.Request.Headers["Accept"].FirstOrDefault()?.Contains("application/json") == true; // Try to parse from the JSON payload first if (jsonRequest && context.Request.ContentLength > 0 && ResultRequest.TryParse(context.Request.Body, out clientRequest) && clientRequest.Id.HasValue) { id = clientRequest.Id.Value; } else if (Guid.TryParse(context.Request.Query["id"], out id)) { // We got the guid from the querystring } else if (Options.StopwatchProvider != null) { // Fall back to the last result id = (await Options.Storage.ListAsync(1).ConfigureAwait(false)).FirstOrDefault(); } if (id == default(Guid)) { return(NotFound(context, jsonRequest ? null : "No GUID id specified on the query string")); } var profiler = await Options.Storage.LoadAsync(id).ConfigureAwait(false); string user = Options.UserIdProvider?.Invoke(context.Request); await Options.Storage.SetViewedAsync(user, id).ConfigureAwait(false); if (profiler == null) { return(NotFound(context, jsonRequest ? null : "No MiniProfiler results found with Id=" + id.ToString())); } bool needsSave = false; if (profiler.ClientTimings == null && clientRequest?.TimingCount > 0) { profiler.ClientTimings = ClientTimings.FromRequest(clientRequest); needsSave = true; } if (!profiler.HasUserViewed) { profiler.HasUserViewed = true; needsSave = true; } if (needsSave) { await Options.Storage.SaveAsync(profiler).ConfigureAwait(false); } if (!AuthorizeRequest(context, isList: false, message: out string authorizeMessage)) { context.Response.ContentType = "application/json"; return(@"""hidden"""); // JSON } if (jsonRequest) { context.Response.ContentType = "application/json"; return(profiler.ToJson()); } else { context.Response.ContentType = "text/html; charset=utf-8"; return(Render.SingleResultHtml(profiler, context.Request.PathBase + Options.RouteBasePath.Value.EnsureTrailingSlash())); } }
public async Task AdddClientTiming() { var resultRequest = await jsRuntime.InvokeAsync <ResultRequest>("MiniProfiler.Blazor.getClientTimings"); MiniProfiler.Current.ClientTimings = ClientTimings.FromRequest(resultRequest); }