Skip to content

Instantly share code, notes, and snippets.

@leekelleher
Last active November 13, 2018 09:29
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 leekelleher/57a4ff10431803b389aad74f59ce3c49 to your computer and use it in GitHub Desktop.
Save leekelleher/57a4ff10431803b389aad74f59ce3c49 to your computer and use it in GitHub Desktop.
Umbraco - an IContentFinder that attempts to find the content by it's ASCII URL. This is useful for if an existing website has switched from non-ASCII URLs.
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
using Umbraco.Web;
using Umbraco.Web.Routing;
namespace Our.Umbraco.Web.Routing.ContentFinders
{
public class ContentFinderByAsciiUrl : IContentFinder
{
public virtual bool TryFindContent(PublishedContentRequest contentRequest)
{
if (contentRequest == null)
return false;
if (!UmbracoConfig.For.UmbracoSettings().RequestHandler.ConvertUrlsToAscii)
return false;
var path = contentRequest.Uri.GetAbsolutePathDecoded();
// if the URL path only has ASCII (ANSI) characters,
// then ignore it! (We're only interested in non-ASCII chars)
if (!path.Any(x => (int)x > 127))
return false;
LogHelper.Debug<ContentFinderByAsciiUrl>("TryFindContent({0})", () => contentRequest.Uri.ToString());
var parts = path.ToDelimitedList("/");
var asciiPath = string.Concat("/", string.Join("/", parts.Select(x => x.ToUrlSegment())), "/");
var route = contentRequest.HasDomain
? string.Concat(contentRequest.UmbracoDomain.RootContentId, DomainHelper.PathRelativeToDomain(contentRequest.DomainUri, asciiPath))
: asciiPath;
var node = contentRequest
.RoutingContext
.UmbracoContext
.ContentCache
.GetByRoute(route);
if (node == null)
return false;
contentRequest.SetRedirectPermanent(node.Url);
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment