Skip to content

Instantly share code, notes, and snippets.

@skttl
Created October 21, 2016 07:15
Show Gist options
  • Save skttl/844fa6ba0cb5c6cb83cb77ed4bfc550b to your computer and use it in GitHub Desktop.
Save skttl/844fa6ba0cb5c6cb83cb77ed4bfc550b to your computer and use it in GitHub Desktop.
Umbraco URL Provider for frontpage set in "the ultimate site structure" (https://cultiv.nl/blog/tip-of-the-week-the-ultimate-site-structure-setup/)
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core;
using Umbraco.Web;
using Umbraco.Web.Routing;
namespace MyWebsite.Core.UrlProviders
{
public class FrontpageUrlProviderApplicationEventHandler : ApplicationEventHandler
{
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
UrlProviderResolver.Current.InsertTypeBefore<DefaultUrlProvider, FrontpageUrlProvider>();
}
}
public class FrontpageUrlProvider : IUrlProvider
{
public string GetUrl(UmbracoContext umbracoContext, int id, Uri current, UrlProviderMode mode)
{
var content = umbracoContext.ContentCache.GetById(id);
if (content != null && content.AncestorOrSelf(1).HasValue("umbracoInternalRedirectId") && content.AncestorOrSelf(1).GetProperty("umbracoInternalRedirectId").DataValue.ToString() == content.Id.ToString())
{
return content.AncestorOrSelf(1).Url;
}
return null;
}
public IEnumerable<string> GetOtherUrls(UmbracoContext umbracoContext, int id, Uri current)
{
return Enumerable.Empty<string>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment