.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Validations >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Generate SEO friendly URL at runtime in asp net

Posted By:Faizal       Posted Date: September 28, 2008    Points: 10    Category: Validations    URL: http://www.dotnetspark.com  
 

This code used to dynamilcally generate SEO Friendly URL at runtime in ASP.NET . This code is written C# which will allow 45 additional character apart from domain name in the URL.

public static string GenerateSEOURL(object Title)
        {
            string strTitle = Title.ToString();
             strTitle = strTitle.ToLower();
            char[] chars = @"$%#@!*?;:~`+=()[]{}|\'<>,/^&"".".ToCharArray();
           
           
            strTitle = strTitle.Replace(".", "");

           

            for (int i = 0; i < chars.Length; i++)
            {
                string strChar = chars.GetValue(i).ToString();
                if (strTitle.Contains(strChar))
                {
                    strTitle = strTitle.Replace(strChar, string.Empty);
                }
            }/

            strTitle = strTitle.Trim();
            strTitle = strTitle.Trim('-');

           //Add the list which you want to exclude from URL
            ArrayList arrList = new ArrayList();
            arrList.Add("to");
            arrList.Add("in");
            arrList.Add("the");
            arrList.Add("a");
            arrList.Add("an");
            arrList.Add("at");
            arrList.Add("or");
            arrList.Add("?");
           
            string[] strTempWords = strTitle.Split(" ".ToCharArray());
            string url = "";
            if (strTempWords.Length > 0)
            {
               
                for (int i = 0; i < strTempWords.Length; i++)
                {
                    if (!arrList.Contains(strTempWords[i]))
                    {
                        if (url.Length < 50)
                        {

                            url +=  strTempWords[i]+" ";
                        }
                    }
                }
                url = url.Remove(url.Length-1, 1);
              
            }
            strTitle = url.Replace(" ", "-");
           

            return strTitle;
        }


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend