Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Configuration;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.Models;
using Umbraco.Web;
using Umbraco.Web.Routing;
namespace Moriyama.Core.Routing.UrlProviders
{
/// <summary>
/// This UrlProvider is aware of Umbraco Hidden Folders so it removes them from the url.
/// </summary>
public class UmbracoHiddenFolderUrlProvider : DefaultUrlProvider
{
public UmbracoHiddenFolderUrlProvider() : base()
{
}
public UmbracoHiddenFolderUrlProvider(IRequestHandlerSection requestSettings) : base(requestSettings)
{
}
public override IEnumerable<string> GetOtherUrls(UmbracoContext umbracoContext, int id, Uri current)
{
return base.GetOtherUrls(umbracoContext, id, current);
}
public override string GetUrl(UmbracoContext umbracoContext, int id, Uri current, UrlProviderMode mode)
{
var umbracoHiddenFolderAlias = WebConfigurationManager.AppSettings["UmbracoHiddenFolderAlias"] ?? "umbracoHiddenFolder";
string defaultUrl = base.GetUrl(umbracoContext, id, current, mode);
IPublishedContent content = umbracoContext.ContentCache.GetById(id);
if (content != null)
{
if (content.DocumentTypeAlias == umbracoHiddenFolderAlias)
{
// For Hidden Folders, we want to prefix them to avoid URL clashes
var folderUrl = "/_folder-" + defaultUrl.TrimStart('/');
return folderUrl;
}
else
{
if (!(defaultUrl.StartsWith("http") || defaultUrl.StartsWith("/")))
{
//it's a message about the URL, not a URL
return defaultUrl;
}
var siteRoot = content.Site();
if (siteRoot == null)
{
return defaultUrl;
}
var firstFolderAncestor = content.Ancestors(umbracoHiddenFolderAlias).FirstOrDefault();
if (firstFolderAncestor != null)
{
// We want to remove the URL of the nearest Hidden Folder from our URL, so it doesn't appear in the URL
var firstFolderAncestorUrl = firstFolderAncestor.Url.Replace("_folder-", string.Empty);
var newUrl = "/" + defaultUrl.Replace(firstFolderAncestorUrl, string.Empty);
return newUrl;
}
}
}
return defaultUrl;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.