Skip to content

Instantly share code, notes, and snippets.

@elucidan
Last active April 22, 2021 09:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elucidan/9a0cda4d615ef4fcc1b1d99bc5d3ecf0 to your computer and use it in GitHub Desktop.
Save elucidan/9a0cda4d615ef4fcc1b1d99bc5d3ecf0 to your computer and use it in GitHub Desktop.
Class to enable umbraco mini profiler on every request, bit hacky and doesn't handle multiple query strings yet
namespace YourNamespace.Core
{
using System;
using System.Collections.Generic;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.Logging;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Web;
using Umbraco.Web.Routing;
using Umbraco.Core.Composing;
public class DebugUrlProvider : DefaultUrlProvider
{
public override UrlInfo GetUrl(UmbracoContext umbracoContext, IPublishedContent content, UrlMode mode,
string culture, Uri current)
{
UrlInfo defaultUrlInfo = base.GetUrl(umbracoContext, content, mode, culture, current);
if (!defaultUrlInfo.IsUrl)
{
return defaultUrlInfo;
}
if (System.Configuration.ConfigurationManager.AppSettings["UseProfilerOnAllPages"] == "true")
{
var originalUrl = defaultUrlInfo.Text;
var customUrl = $"{originalUrl}{(originalUrl.Contains("?") ? "&":"?")}umbDebug=true";
return new UrlInfo(customUrl, true, defaultUrlInfo.Culture);
}
return base.GetUrl(umbracoContext, content, mode, culture, current);
}
public DebugUrlProvider(IRequestHandlerSection requestSettings, ILogger logger, IGlobalSettings globalSettings,
ISiteDomainHelper siteDomainHelper) : base(requestSettings, logger, globalSettings, siteDomainHelper)
{
}
}
public class RegisterCustomUrlProviderComposer : IUserComposer
{
public void Compose(Composition composition)
{
composition.UrlProviders().Insert<DebugUrlProvider>();
}
}
}
<!-- add this to your app settings -->
<add key="UseProfilerOnAllPages" value="true" />
@ccasalicchio
Copy link

ccasalicchio commented Apr 22, 2021

One suggestion, umb profiler works even if the URL has / in it... so instead of
var customUrl = originalUrl.TrimEnd('/') + "?umbDebug=true";

maybe it will work on all URLs, including query strings by just doing
var customUrl = $"{originalUrl}{(originalUrl.Contains("?"):"&":"?")}umbDebug=true";

@elucidan
Copy link
Author

One suggestion, umb profiler works even if the URL has / in it... so instead of
var customUrl = originalUrl.TrimEnd('/') + "?umbDebug=true";

maybe it will work on all URLs, including query strings by just doing
var customUrl = $"{originalUrl}{(originalUrl.Contains("?"):"&":"?")}umbDebug=true";

Good shout, updated it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment