Created
June 1, 2017 12:01
-
-
Save mikkelhm/85dc7c2d3b5cfd3114b733b3c76748d6 to your computer and use it in GitHub Desktop.
Umbraco Bypass ascii encoding if all fails
This file contains hidden or 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
using System.Globalization; | |
using Umbraco.Core; | |
using Umbraco.Core.Configuration; | |
using Umbraco.Core.Configuration.UmbracoSettings; | |
using Umbraco.Core.Strings; | |
namespace Custom | |
{ | |
public class CustomApplicationEventHandler : ApplicationEventHandler | |
{ | |
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) | |
{ | |
var shortStringHelper = new CustomShortStringHelper(UmbracoConfig.For.UmbracoSettings()).WithDefaultConfig(); | |
ShortStringHelperResolver.Current.SetHelper(shortStringHelper); | |
base.ApplicationStarting(umbracoApplication, applicationContext); | |
} | |
} | |
} |
This file contains hidden or 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
using System.Globalization; | |
using Umbraco.Core; | |
using Umbraco.Core.Configuration; | |
using Umbraco.Core.Configuration.UmbracoSettings; | |
using Umbraco.Core.Strings; | |
namespace Custom | |
{ | |
public class CustomShortStringHelper : DefaultShortStringHelper | |
{ | |
public CustomShortStringHelper(IUmbracoSettingsSection umbracoSettings) : base(umbracoSettings) { } | |
public override string CleanStringForUrlSegment(string text) | |
{ | |
var converted = this.CleanString(text, CleanStringType.UrlSegment); | |
if (string.IsNullOrWhiteSpace(converted)) | |
{ | |
return text; | |
} | |
return converted; | |
} | |
public override string CleanStringForUrlSegment(string text, CultureInfo culture) | |
{ | |
var converted = this.CleanString(text, CleanStringType.UrlSegment, culture); | |
if (string.IsNullOrWhiteSpace(converted)) | |
{ | |
return text; | |
} | |
return converted; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Used if i.e. you need to have toAscii set to true, while some nodes might not be able to be translated into Ascii, like chinese.
This will simply not do any conversions on the strings, if they contain non ascii letters, i.e. the CleanString method returned empty, which it would if toAscii is on, and the node is in chinese.