Generate SEO Friendly URL from given string in C# for Asp.net Web Application.
Example 1: Simple Test
- Input: CSS is not working in IE? but working in FireFox... asp.net problem
- Output: css-is-not-working-in-ie-but-working-in-firefox-Asp-Net-problem
Example 2: Spaces Testing
- Input: asp.net problem url re-writing.... testing the basics
- Output:
Untitled Page Untitled Page Asp-Net-problem-url-re-writing-testing-the-basics
- Input: 23 asp.net problem ### url re-writing.... testing the basics will pay $1000
- Output:
Untitled Page Untitled Page Untitled Page 23-Asp-Net-problem-url-re-writing-testing-the-basics-will-pay-1000
private string GenerateURL(string strTitle)
{
//Trim Start and End Spaces.
strTitle = strTitle.Trim();
//Trim "-" Hyphen
strTitle = strTitle.Trim('-');
strTitle = strTitle.ToLower();
char[] chars = @"$%#@!*?;:~`+=()[]{}|\'<>,/^&"".".ToCharArray();
strTitle = strTitle.Replace("c#", "C-Sharp");
strTitle = strTitle.Replace("vb.net", "VB-Net");
strTitle = strTitle.Replace("asp.net", "Asp-Net");
//Replace . with - hyphen
strTitle = strTitle.Replace(".", "-");
//Replace Special-Characters
for (int i = 0; i < chars.Length; i++)
{
string strChar = chars.GetValue(i).ToString();
if (strTitle.Contains(strChar))
{
strTitle = strTitle.Replace(strChar,string.Empty);
}
}
//Replace all spaces with one "-" hyphen
strTitle = strTitle.Replace(" ", "-");
//Replace multiple "-" hyphen with single "-" hyphen.
strTitle = strTitle.Replace("--", "-");
strTitle = strTitle.Replace("---", "-");
strTitle = strTitle.Replace("----", "-");
strTitle = strTitle.Replace("-----", "-");
strTitle = strTitle.Replace("----", "-");
strTitle = strTitle.Replace("---", "-");
strTitle = strTitle.Replace("--", "-");
//Run the code again...
//Trim Start and End Spaces.
strTitle = strTitle.Trim();
//Trim "-" Hyphen
strTitle = strTitle.Trim('-');
return strTitle;
}
7 comments:
Isn't there some sort of regular expression that can do this in a single statement?
Yes regular expression is the best way to tackle this, but if you focus on code logic, you wil feel it would consume more time to develop this stuff with regular-expression (as I am not good with it).
Check out article on URL Rewriting Simplest Article for URL Rewriting
Using string.replace multiple times in ADDITION to the loop seems a bit expensive.
Take a look at http://horizonguy.wordpress.com/2008/10/18/optimize-a-url-for-seo-using-c
It's using hash table for char lookups and buffer for building the url.
try this:
using System.Text.RegularExpressions;
public string GenerateURL(string str,bool toLowerCase)
{
//Replace . with - hyphen
str = Regex.Replace(str.Trim(), @"\W", "-");
//Replace multiple "-" hyphen with single "-" hyphen.
str = Regex.Replace(str, @"\055+", "-").Trim('-');
if (toLowerCase)
return str.ToLower();
else
return str;
}
Hi Ray,
Your solution is helpful and easy. Thanks :)
I just created simple function.
public static string createURL(string m_strURL)
{
//m_strURL = m_strURL.Replace("&", "and");
m_strURL = Regex.Replace(m_strURL, @"[^\w]", "-");
do
{
m_strURL = m_strURL.Replace("--", "-");
} while (m_strURL.Contains("--"));
if (m_strURL.EndsWith("-"))
m_strURL = m_strURL.Substring(0, m_strURL.Length - 1);
if (m_strURL.StartsWith("-"))
m_strURL = m_strURL.Substring(1, m_strURL.Length - 1);
return m_strURL.ToLower();
}
Post a Comment