Skip to content

Instantly share code, notes, and snippets.

@dampee
Created June 13, 2014 11:09
Show Gist options
  • Save dampee/4b4fb227a86ce64e4155 to your computer and use it in GitHub Desktop.
Save dampee/4b4fb227a86ce64e4155 to your computer and use it in GitHub Desktop.
example segment provider
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Strings;
public class MyApplication : ApplicationEventHandler
{
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
base.ApplicationStarting(umbracoApplication, applicationContext);
// UrlSegmentProviderResolver.Current.Clear();
UrlSegmentProviderResolver.Current.InsertType<ImmoUrlSegmentProvider>(0);
}
}
/// <summary>
/// Summary description for ImmoUrlSegmentProvider
/// </summary>
public class ImmoUrlSegmentProvider : IUrlSegmentProvider
{
/// <summary>
/// The provider.
/// </summary>
private readonly IUrlSegmentProvider provider = new DefaultUrlSegmentProvider();
/// <summary>
/// Gets the default url segment for a specified content.
/// </summary>
/// <param name="content">The content.</param>
/// <returns>The url segment.</returns>
public string GetUrlSegment(IContentBase content)
{
return this.GetUrlSegment(content, CultureInfo.CurrentCulture);
}
/// <summary>
/// Gets the default url segment for a specified content.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="culture">The culture.</param>
/// <returns>The url segment.</returns>
/// <remarks>This is for when Umbraco is capable of managing more than one url
/// per content, in 1-to-1 multilingual configurations. Then there would be one
/// url per culture.</remarks>
public string GetUrlSegment(IContentBase content, CultureInfo culture)
{
if (content.ContentTypeId != 1086)
{
return null;
}
var segment = this.provider.GetUrlSegment(content);
// return string.Format("{0}-{1}-{2}", content.Id, segment, content.GetValue("city"));
return string.Format("{0}-{1}-{2}", content.GetValue("typeDescription"), content.GetValue<string>("city"), segment).ToUrlSegment();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment