ASPX templates
Version 5.x > ASPX templates > Replace Forbidden Characters Method View modes: 
User avatar
Member
Member
eric.rovtar - 3/9/2011 9:11:21 AM
   
Replace Forbidden Characters Method
Hi!

Sorry, this is probably an easy question, but what is the API call to replace all forbidden characters in a string based on the Forbidden URL Characters setting in SiteManager?

Thanks!

User avatar
Member
Member
kentico_michal - 3/15/2011 7:23:08 AM
   
RE:Replace Forbidden Characters Method
Hi Eric,

Regrettably, Kentico CMS does not provide any general method to get safe string text. However, there are two methods to get safe file name and part of url:

CMS.GlobalHelper.UrlHelper.GetSafeFileName
CMS.GlobalHelper.UrlHelper.GetSafeUrlPart


Moreover, there should not be any problem to create your custom method, which could get Forbidden URL characters and Forbidden characters replacement values using following method:

CMS.GlobalHelper.UrlHelper.ForbiddenURLCharacters
CMS.GlobalHelper.UrlHelper.ForbiddenCharactersReplacement


and could replace the forbidden characters in the text. So in general, this method could look as follows:


public string GetSafeString(string text, string siteName)
{
// If text is not specified, do not process
if (text == null)
{
return "";
}

// Remove diacritics from the url
text = TextHelper.RemoveDiacritics(text.Trim());

// Get site forbidden character and repalcement
string forbiddenChars = CMS.GlobalHelper.UrlHelper.ForbiddenURLCharacters(siteName);
char replacement = CMS.GlobalHelper.UrlHelper.ForbiddenCharactersReplacement(siteName);
string repalcementString = replacement.ToString();

// Replace the forbidden characters
for (int i = 0; i <= forbiddenChars.Length - 1; i++)
{
text = text.Replace(forbiddenChars, replacement);
}

return text;
}



Best regards,
Michal Legen

User avatar
Member
Member
dlents-coleyfederal - 6/6/2011 2:44:02 PM
   
RE:Replace Forbidden Characters Method
Here is a slightly different version.

public static string GetSafeString(string text, string siteName)
{
// If text is not specified, do not process
if (string.IsNullOrEmpty(text))
{
return "";
}

// Remove diacritics from the url
text = TextHelper.RemoveDiacritics(text.Trim());

// Get site forbidden character and repalcement
string strForbiddenChars = CMS.GlobalHelper.UrlHelper.ForbiddenURLCharacters(siteName);
char[] ForbiddenChars = strForbiddenChars.ToCharArray();
char replacement = CMS.GlobalHelper.UrlHelper.ForbiddenCharactersReplacement(siteName);

// Replace the forbidden characters
for (int i = 0; i <= ForbiddenChars.Length - 1; i++)
{
text = text.Replace(ForbiddenChars, replacement);
}

return text;
}