Is this really the version you are looking for? Kentico Xperience 12 Service Pack Documentation is no longer updated. Use the switcher in the header to choose your product version.
To ensure that URLs can be processed correctly, the system checks text values for unsafe characters before they are added to the URL path, and makes any necessary changes. This affects fields such as page aliases, page URL paths, forum names, post subjects etc.
By default, the process includes removal of diacritics and replacement of all forbidden characters according to the settings defined for the given site (as described in URL format and configuration). Characters with diacritics are replaced by the appropriate base character, for example ü is converted to u.
If you need to change the default behavior in any way, you can register and implement a handler for one or both of the following system events:
The events are members of the URLHelper and TextHelper classes respectively, which can be found under the CMS.Helpers namespace.
Warning
Only return a false value if you are sure that your custom handler can take full responsibility for all URL safety or diacritics removal requirements.
Disabling the default system functionality may prevent parts of your website from working correctly, particularly in the case of handlers for the OnBeforeGetSafeUrlPart event.
The following example demonstrates how to define handlers for the forbidden character events:
Open your Kentico web project in Visual Studio.
Create a custom module class.
Override the module's OnInit method and assign handlers to the required events:
using System;
using CMS;
using CMS.DataEngine;
using CMS.Helpers;
// Registers the custom module into the system
[assembly: RegisterModule(typeof(CustomURLPathModule))]
public class CustomURLPathModule : Module
{
// Module class constructor, the system registers the module under the name "CustomURLPathHandling"
public CustomURLPathModule()
: base("CustomURLPathHandling")
{
}
// Contains initialization code that is executed when the application starts
protected override void OnInit()
{
base.OnInit();
// Assigns a handler to the OnBeforeGetSafeURLPart event
URLHelper.OnBeforeGetSafeUrlPart += Custom_OnBeforeGetSafeUrlPart;
// Assigns a handler to the OnBeforeRemoveDiacritics event
TextHelper.OnBeforeRemoveDiacritics += Custom_OnBeforeRemoveDiacritics;
}
}The system applies the changes when creating new URLs or page aliases, and when removing diacritics from text.
Note: The customization does not automatically change the aliases and URL paths of existing pages until the current value is changed or saved.
Add the definition of the Custom_OnBeforeGetSafeUrlPart handler into the module class:
static bool Custom_OnBeforeGetSafeUrlPart(ref string url, string siteName, EventArgs e)
{
// Replaces all & characters with the word "and"
url = url.Replace("&", "and");
// Returns true to indicate that the default URL replacements should also be performed - removing forbidden characters and diacritics (both default and custom)
return true;
}This code replaces all ampersand (&) characters with the word and. For example, a page named Products & Services has its default page alias generated as Products-and-Services, which is then used in the page's URL. This example is only meant as a simple demonstration. In real‑world scenarios, the handler can be much more complex, for example when mapping the character set of an international language to appropriate ASCII characters or words.
The siteName parameter of the handler contains the code name of the site under which the event occurred. This can be useful if you need to access the forbidden character settings of the given site in your custom code.
Add the Custom_OnBeforeRemoveDiacritics handler:
static bool Custom_OnBeforeRemoveDiacritics(ref string text, EventArgs e)
{
// Replaces German special characters
text = text.Replace( "ä", "ae" );
text = text.Replace( "ö", "oe" );
text = text.Replace( "ü", "ue" );
text = text.Replace( "Ä", "Ae" );
text = text.Replace( "Ö", "Oe" );
text = text.Replace( "Ü", "Ue" );
text = text.Replace( "ß", "ss" );
// Returns true to indicate that the default diacritics removal should also be performed
return true;
}This ensures that the system uses custom replacements for German special characters rather than simply stripping the diacritics and leaving the base character.
Save the file (Build your project if it was installed as a web application).