Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to generate render time keywords for the search bots by ASP.Net

0.00/5 (No votes)
27 Jan 2008 1  
How to generate render time keywords for the search bots by ASP.Net

Introduction

It is very important to be findable on search engines if you want to get many hits. And usually all search engines, use the "keywords" meta tag of your page.. But, the search bots check the keywords tag and if the content of keywords is included in body. With this filter, your page will have pre-produced keywords tag.

Using the code

Actually it is a copy-paste code.. Only create a class in app_code folder. and copy - paste the following lines

public class cdsnetKeywordGenerator : Stream
{
    private MemoryStream _sink;// this is our real buffer to copy page
    private Stream _sink2; // this is out output buffer
    private long _position; 
    public cdsnetKeywordGenerator(Stream sink)
    {
        _sink2 = sink; // copy old filter or buffer
        _sink = new MemoryStream(); // create a new buffer
    }

    public override bool CanRead
    {
        get { return true; }
    }

    public override bool CanSeek
    {
        get { return true; }
    }

    public override bool CanWrite
    {
        get { return true; }
    }

    public override long Length
    {
        get { return 0; }
    }

    public override long Position
    {
        get { return _position; }
        set { _position = value; }
    }

    public override long Seek(long offset, System.IO.SeekOrigin direction)
    {
        return _sink.Seek(offset, direction);
    }

    public override void SetLength(long length)
    {
        _sink.SetLength(length);
    }

    public override void Close()
    {
        _sink.Close();
    }

    public override void Flush()
    {
        _sink.Flush();
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        int t = _sink.Read(buffer, offset, count);
        return t;
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        //First of all let's convert the buffered data to string. 
        //So we can use UTF8 Encoder to bytearray-string convertion
        string s = Encoding.UTF8.GetString(buffer);
        //Create a regular expression to find if the closing html tag
        Regex regBody = new Regex("</html>");
        //if we have </html> tag on current buffer.. that mean is
        //page rendering is completed.
        if (regBody.IsMatch(s))
        {
            //Let's write the last buffered data to our copier stream
            HttpContext.Current.Response.Buffer = true;
            _sink.Write(buffer, 0, count);

            //Let's convert our buffered data to string
            s = Encoding.UTF8.GetString(_sink.ToArray());
            string t = HttpUtility.HtmlDecode(s);
            Regex reg;

            //following lines are optional for if u want to generate keywords
            //of your script tags
            //reg = new Regex(@"<script[^>]*>[\d\D]*?</script>", RegexOptions.Multiline | RegexOptions.IgnoreCase);
            //while (reg.IsMatch(t))
            //    t = reg.Replace(t, "");

            //Let's create a regular expression to determine the tags
            //and replace them with space. With this regex any tags
            //like image, input or etc. will be ignored
            //so any unnecessary variables like viewstate will be discarded
            reg = new Regex(@"<[A-Z0-9/!]{1,}[^>]*>", RegexOptions.Multiline | RegexOptions.IgnoreCase);
            t = reg.Replace(t, " ");

            //Now we have words, signs and numbers.. but we need only words
            //.. so we ll create a regular expression
            //what will look for non-words and numbers
            //then we ll replace them by a seperator keyword 
            //i used "tlghn" as seperator in here
            reg = new Regex(@"[\W\d]+", RegexOptions.Multiline | RegexOptions.IgnoreCase);
            while (reg.IsMatch(t))
                t = reg.Replace(t, "tlghn");
            
            //now we ll split the words by "tlghn" 
            List liste = new List();
            string[] tt = t.ToLowerInvariant().Split(new string[] { "tlghn" },
 StringSplitOptions.RemoveEmptyEntries);

            //to use unique keywords we ll save them in a list
            foreach(string sst in tt)
            {
                if (!liste.Contains(sst)&&sst.Length>3) 
                    liste.Add(sst);
            }

            //in this area you can add your optional keywords
            liste.Add("Tolgahan Albayrak");
            liste.Add("CDSNet");
            liste.Add("CDSNet Bilisim Teknolojileri");
            liste.Add("CDSNet TR");
            liste.Add("CDSNetTR");
            liste.Add("http://www.cdsnettr.com");

            //let's sort keywords that is not necessary
            liste.Sort();
            //now let's join the string members with "," comma
            t = string.Join(",", liste.ToArray());

            //let's find the old keywords tag and delete it
            reg = new Regex("<meta.*\"keywords\"[^>]*>", RegexOptions.IgnoreCase);
            s = reg.Replace(s, "");

            //let's find the closing head tag and replace it by our keywords 
            //and a closing head tag
            reg = new Regex(@"</head>");
            s = reg.Replace(s, 
            string.Format("\n<!--This Keywords Automatically Generated By" +
" Tolgahan Albayrak's ASP.Net Keyword Generator Filter//-->\n<meta" +
" name=\"keywords\" content=\"{0}\" ></head>", t));

            //finally let's convert output to byte and lets write it to 
            //original stream
            byte[] bt = Encoding.UTF8.GetBytes(s);
            _sink2.Write(bt, 0, bt.Length);
            
        }
        else
        {
            //there is no closing html tag so we must copy the data to our buffer stream
            _sink.Write(buffer, 0, count);
        }
    }
}
		

And configure your page which will be filtered as below

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Response.Filter = new cdsnetKeywordGenerator (this.Response.Filter);
    }

Result

I used this this class in a turkish estate company's web site.. here is the result of default page
<!--This Keywords Automatically Generated By Tolgahan
 Albayrak's ASP.Net Keyword Generator Filter//-->
<meta name="keywords" content="___bodystartheight,
___resizeyeni,___troliste,___yeniemlakfonk,__obj,
_cdsnettronclick,_cphinner_cdsnetnewestate,_cphinner_cdsnetvitrin,_iddiv,
_yeniemlakcont,adakale,agar,akmescit,alan,alani,alaybey,alibey,anafartalar,
anemon,araligi,arama,arazi,arda,arkadasa,arkadasima,arkasi,arsa,
asagikayapinar,ayakkabicilar,aydin,ayrintilar,ayrintilari,baba,bah�elievler,
bana,baris,bedel,behavior,benzer,birlik,body,b�lgesi,bulundugu,bursa,cami,camii,
CDSNet,CDSNet Bilisim Teknolojileri,CDSNet TR,cdsnetaddevent,CDSNetTR,
cdsnetverticalnewsticker,cemal,civari,classname,click,�elebi,�ikar,�inari,
�inarli,�iftligi,�iftlik,daire,davutlar,dering�z,document,dubleks,d�kkan,
efendi,egemah,ekle,ekmek,emlak,erg�n,fabrika,fatih,fenerbah�eliler,firini,
firsati,fiyat,fiyati,fotograflilar,function,gayrimenkul,gayrimenkulleri,
gayrimenkul�,germen,getelementbyid,getelementsbytagname,g�k�eler,g�nder,
g�ster,g�sterme,g�n�n,g�rle,g�zelbah�e,g�zelyurt,hacirahmanli,hafsa,haki,
hasim,height,hemen,horozk�y,href,http://www.cdsnettr.com,Ibrahim,i�in,I�in,
iepngfix,Il�e,Ilk�gretim,innertext,Istanbul,Istasyon,Islemler,izmir,Izmir,
kahve,karadeniz,karakolu,karak�y,karsisi,kayit,kaynak,ke�ili,kiralik,konum,
koop,k�m�rc�ler,k�y�,kriterleri,kusadasi,lalapasa,lale,length,lisesi,listem,
listemdekileri,listemden,listeme,listemi,load,location,lokali,manisa,math,
merkez,mesir,mevki,mevlana,meydani,mimar,mimarsinan,mollasaban,muradiye,
muratgermen,mutlu,nazar,necatibey,offsetheight,organize,orta,�l�ekli,�teli,
pamucak,parentnode,parki,peker,pinarbasi,polis,postama,postasi,reset,resim,
resize,sadece,sakarya,sanayi,saruhan,saruhanli,satilik,sayfada,screen,script,
se�iniz,seferihisar,sel�uk,serel,sifir,sinan,sitesi,sonu�lari,sorgu,style,
sultan,s�l�tl��esme,s�rat,sakran,sehitler,tagname,talep,tekn,tekstilciler,
temizle,text,tiklayiniz,Tolgahan Albayrak,tolowercase,top�uasim,tribleks,
tripleks,true,tunca,t�r�,uncubozk�y,urla,�niv,�zeri,villa,vitrin,while,
window,yakini,yani,yapi,yazdir,yazlik,yeni,yeniler,yenimanisa,yola,yolla,
yolu,yukari�obanisa,yunus,y�ksek,zemin,zeytinlik" >

And the page's generation time is 0.02 sec

That is all.. Enjoy!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here