Last active
August 10, 2018 01:51
-
-
Save Swimburger/866b92895bc81db7086c811b7a10033f to your computer and use it in GitHub Desktop.
BlogPostUrlProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BlogPostUrlProvider : IUrlProvider | |
{ | |
public IEnumerable<string> GetOtherUrls(UmbracoContext umbracoContext, int id, Uri current) | |
{ | |
return Enumerable.Empty<string>(); | |
} | |
public string GetUrl(UmbracoContext umbracoContext, int id, Uri current, UrlProviderMode mode) | |
{ | |
var content = umbracoContext.ContentCache.GetById(id); | |
if (content == null || content.DocumentTypeAlias != "blogPostPage") | |
{ | |
return null; | |
} | |
var blogRootUrl = umbracoContext.UrlProvider.GetUrl(content.Parent.Id, current, mode); | |
var category = content.GetPropertyValue<string>("category")?.Slug(); | |
return $"{blogRootUrl}{category}/{content.UrlName}"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RegisterBlogPostUrlProviderEvent : ApplicationEventHandler | |
{ | |
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) | |
{ | |
UrlProviderResolver.Current.InsertTypeBefore<DefaultUrlProvider, BlogPostUrlProvider>(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment