Portal Engine Questions on portal engine and web parts.
Version 5.x > Portal Engine > Kentico Ugly Document Url View modes: 
User avatar
Member
Member
luke - 10/16/2011 8:31:02 PM
   
Kentico Ugly Document Url
More of a request than anything as its the way the Kentico urls are cleaned internally is really ugly
having urls such as /Hotels--Spas
from the document name of "Hotels and Spas" really grinds my gears.
What would be nice is /Hotels-and-Spas
See code below for example on how to generate them


/// <summary>
/// Will convert the string (name) to a url friendly version.
/// Also see UriHelper.GenerateSlug(...)
/// e.g.
/// Example Name => example-name
/// Example& Name => exampleand-name
/// Examplé-test-Name => example-test-name
/// ExamplÅ Name => exampla-name
/// Example Name... => example-name
/// Example & Name! => example-and-name
/// Example & Name! => example-and-name
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static string GenerateFriendlyName(string name)
{
// 1. Convert to base letters e.g. é = e, Â = A
string str = RemoveDiacritics(name);
// 2. Once converted, make lowercase
str = str.ToLower();
// 3. Decode (incase html) e.g & = &, © = ©
str = HttpUtility.HtmlDecode(str);
// 4. Since '&' will be removed, we instead want to convert to base letters
str = str.Replace("&", "and");
// 5. Replace invalid chars with spaces
str = Regex.Replace(str, @"[^a-z0-9s-]", " ").Trim();
// 6. Convert multiple spaces/hyphens into one space
str = Regex.Replace(str, @"\s+", "-");
str = Regex.Replace(str, @"-{2,}", "-");
return str;
}

User avatar
Kentico Support
Kentico Support
janh-kentico - 10/17/2011 1:50:16 AM
   
RE:Kentico Ugly Document Url
Hello Luke,

The ampersand character is not allowed character in a document path.

If you want to replace it with an and word (or if you want to change our alias document name generation to your own one), you have to customize the Save method, which is saving a new document.

This method is located in the NewPage.aspx.cs file in the ~\55R2\CMSModules\Content\CMSDesk\New\, so here you can generate your own alias path:

Around the line 300:
TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);
TreeNode node = new TreeNode("CMS.MenuItem", tree);

// Load default data
FormHelper.LoadDefaultValues(node);

node.DocumentName = newPageName;



node.NodeAlias = Regex.Replace(newPageName, "&", "and");


node.SetValue("MenuItemName", newPageName);
lblError.Style.Remove("display");

Best regards,
Jan Hermann