Tweak to SiteUtils SuggestFriendlyUrl

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
10/28/2011 3:37:45 PM
Gravatar
Total Posts 76

Tweak to SiteUtils SuggestFriendlyUrl

My users are not the smartest bunch, So we have some pages and blogs with the same titles (ugg) and i notices url service threw a 1 in front of the url...

I know that having the # are better at the end for SEO so i looked to move to end...  and saw a bug in the name gen.

It would keep adding a 1 to the front of the urls ( i know this is rare / and undesirable )

so the progression would be:

~/Friendly-Url.aspx
~/1Friendly-Url.aspx
~/11Friendly-Url.aspx

I would prefer:

~/Friendly-Url.aspx
~/Friendly-Url-1.aspx
~/Friendly-Url-2.aspx

Here is the code I tested to move the number behind the text and to increment correctly:

public static String SuggestFriendlyUrl(
            String pageName,
            SiteSettings siteSettings)
        {
            String friendlyUrl = CleanStringForUrl(pageName);
            if (WebConfigSettings.AlwaysUrlEncode)
            {
                friendlyUrl = HttpUtility.UrlEncode(friendlyUrl);
            }


            string urlTail = string.Empty;
            switch (siteSettings.DefaultFriendlyUrlPattern)
            {
              case SiteSettings.FriendlyUrlPattern.PageNameWithDotASPX:
                urlTail = ".aspx";
                break;

            }

            var tempFriendlyUrl = friendlyUrl + urlTail;
            int i = 1;
            while (FriendlyUrl.Exists(siteSettings.SiteId, tempFriendlyUrl))
            {
              tempFriendlyUrl = friendlyUrl + "-" + i.ToString() + urlTail;
              i++;
            }
            friendlyUrl = tempFriendlyUrl;


            if (WebConfigSettings.ForceFriendlyUrlsToLowerCase) { return friendlyUrl.ToLower(); }

            return friendlyUrl;
        }

On a side note: i also added a Trim to the CleanStringForUrl

public static String CleanStringForUrl(String input)
{     String outputString = RemovePunctuation(input.Trim())........

11/3/2011 4:18:55 PM
Gravatar
Total Posts 76

Re: Tweak to SiteUtils SuggestFriendlyUrl

Found out how StackOverflow handles suggesting friendly urls... prob worth looking into as it handles titles with some international characters and other strange chars.

 

http://stackoverflow.com/questions/25259/how-do-you-include-a-webpage-title-as-part-of-a-webpage-url/25486#25486

http://meta.stackoverflow.com/questions/7435/non-us-ascii-characters-dropped-from-full-profile-url/7696#7696

/// <summary>
/// Remap International Chars To Ascii
/// http://meta.stackoverflow.com/questions/7435/non-us-ascii-characters-dropped-from-full-profile-url/7696#7696
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
public static string RemapInternationalCharToAscii(char c)
{
  string s = c.ToString().ToLowerInvariant();
  if ("àåáâäãåą".Contains(s))
  {
    return "a";
  }
  else if ("èéêëę".Contains(s))
  {
    return "e";
  }
  else if ("ìíîïı".Contains(s))
  {
    return "i";
  }
  else if ("òóôõöøőð".Contains(s))
  {
    return "o";
  }
  else if ("ùúûüŭů".Contains(s))
  {
    return "u";
  }
  else if ("çćčĉ".Contains(s))
  {
    return "c";
  }
  else if ("żźž".Contains(s))
  {
    return "z";
  }
  else if ("śşšŝ".Contains(s))
  {
    return "s";
  }
  else if ("ñń".Contains(s))
  {
    return "n";
  }
  else if ("ýÿ".Contains(s))
  {
    return "y";
  }
  else if ("ğĝ".Contains(s))
  {
    return "g";
  }
  else if (c == 'ř')
  {
    return "r";
  }
  else if (c == 'ł')
  {
    return "l";
  }
  else if (c == 'đ')
  {
    return "d";
  }
  else if (c == 'ß')
  {
    return "ss";
  }
  else if (c == 'Þ')
  {
    return "th";
  }
  else if (c == 'ĥ')
  {
    return "h";
  }
  else if (c == 'ĵ')
  {
    return "j";
  }
  else
  {
    return "";
  }
}


/// <summary>
/// Produces optional, URL-friendly version of a title, "like-this-one".
/// hand-tuned for speed, reflects performance refactoring contributed by John Gietzen (user otac0n)
/// http://stackoverflow.com/questions/25259/how-do-you-include-a-webpage-title-as-part-of-a-webpage-url/25486#25486
/// </summary>
public static string URLFriendly(string title)
{
  if (title == null) return "";

  const int maxlen = 80;
  int len = title.Length;
  bool prevdash = false;
  var sb = new StringBuilder(len);
  char c;

  for (int i = 0; i < len; i++)
  {
    c = title[i];
    if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'))
    {
      sb.Append(c);
      prevdash = false;
    }
    else if (c >= 'A' && c <= 'Z')
    {
      // tricky way to convert to lowercase
      sb.Append((char)(c | 32));
      prevdash = false;
    }
    else if (c == ' ' || c == ',' || c == '.' || c == '/' || c == '\\' || c == '-' || c == '_' || c == '=')
    {
      if (!prevdash && sb.Length > 0)
      {
        sb.Append('-');
        prevdash = true;
      }
    }
    else if ((int)c >= 128)
    {
      int prevlen = sb.Length;
      sb.Append(RemapInternationalCharToAscii(c));
      if (prevlen != sb.Length) prevdash = false;
    }
    if (i == maxlen) break;
  }

  if (prevdash)
    return sb.ToString().Substring(0, sb.Length - 1);
  else
    return sb.ToString();
}

11/4/2011 9:50:38 AM
Gravatar
Total Posts 18439

Re: Tweak to SiteUtils SuggestFriendlyUrl

Hi Warner,

Your first suggestion for improving friendly url suggest is now in the source code repository. I will also look into your second suggestion about handling non ascii chars.

Thanks,

Joe

11/4/2011 10:07:54 AM
Gravatar
Total Posts 76

Re: Tweak to SiteUtils SuggestFriendlyUrl

Thanks Joe,

I know my site has been affected by some of the weird chars that my users have been able to put un urls,

I like how Jeff Atwood's (and stackoverflow products) try to convert the chars, if there is no conversion it strips it out.

My users find the strangest chars in MS Word and throw them in titles...

:)

11/4/2011 1:59:10 PM
Gravatar
Total Posts 76

Re: Tweak to SiteUtils SuggestFriendlyUrl

Looking at the code in the repo:

int i = 1;
while (FriendlyUrl.Exists(siteSettings.SiteId, friendlyUrl + urlTail))
{
      friendlyUrl = friendlyUrl + "-" + i.ToString();
      i++;
}

You could still get a problem if there are more than 2,

friendly.aspx -> friendly-1.aspx -> friendly-1-2.aspx  -> friendly-1-2-3.aspx   :(

-----

string tempFriendlyUrl = friendlyUrl + urlTail;
int i = 1;
while (mojoPortal.Business.FriendlyUrl.Exists(siteSettings.SiteId, tempFriendlyUrl))
{
    tempFriendlyUrl = friendlyUrl + "-" + i.ToString() + urlTail;
    i++;
}
friendlyUrl = tempFriendlyUrl;

----

friendly.aspx -> friendly-1.aspx -> friendly-2.aspx -> friendly-3.aspx  :)

 

11/4/2011 2:09:27 PM
Gravatar
Total Posts 18439

Re: Tweak to SiteUtils SuggestFriendlyUrl

You are right, I will fix it.

Best,

Joe

11/4/2011 2:47:48 PM
Gravatar
Total Posts 18439

Re: Tweak to SiteUtils SuggestFriendlyUrl

I've updated the code in the repository again with this change. I also used the code for converting non ascii chars, but to test that you'll need to add this to your user.config:

<add key="UseClosestAsciiCharsForUrls" value="true" />

Best,

Joe

11/4/2011 4:33:04 PM
Gravatar
Total Posts 125

Re: Tweak to SiteUtils SuggestFriendlyUrl

<add key="UseClosestAsciiCharsForUrls" value="true" /> 

I just tried this key with Chinese Characters and it seems not working well. 

I changed the page name several times with different Chinese characters and the url change between .aspx and -1.aspx alternatively. 

 

11/5/2011 7:36:39 AM
Gravatar
Total Posts 18439

Re: Tweak to SiteUtils SuggestFriendlyUrl

Hi Wei Li,

This setting is only useful for languages with an alphabet similar to English. As far as I know there is no nearest ascii equivalent to Chinese characters so it cannot work for Chinese and other languages with different character sets.

This is not a new setting, it has existed for a while but it hasn't worked well even for languages with similar alphabets, all I have done is change the implementation to something that will hopefully be a little better than what we had before.

Best,

Joe

You must sign in to post in the forums. This thread is closed to new posts.