Click here to Skip to main content
Licence GPL3
First Posted 27 Jan 2008
Views 20,956
Bookmarked 16 times

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

By | 27 Jan 2008 | Article
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<string> liste = new List<string>();
            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, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

About the Author

Tolgahan ALBAYRAK

Software Developer (Senior)

Turkey Turkey

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralSEO - Blackhat PinmemberLibra6511:46 28 Jan '08  
Generalamount of keywords Pinmemberxcraft9:25 28 Jan '08  
GeneralRe: amount of keywords PinmemberTolgahan ALBAYRAK10:10 28 Jan '08  
GeneralRe: amount of keywords Pinmemberxcraft11:52 28 Jan '08  
GeneralRe: amount of keywords Pinmemberdarkstar3d7:51 29 Jan '08  
GeneralSeriously, though PinmemberMaxGuernsey5:52 28 Jan '08  
GeneralRe: Seriously, though PinmemberSpiff Dog8:41 28 Jan '08  
GeneralRe: Seriously, though PinmemberTolgahan ALBAYRAK8:45 28 Jan '08  
JokeChildren... please! PinmemberMaxGuernsey5:48 28 Jan '08  
GeneralRe: Children... please! PinmemberTolgahan ALBAYRAK8:47 28 Jan '08  
GeneralProject PinmemberDewey12:59 27 Jan '08  
GeneralRe: Project PinmemberTolgahan ALBAYRAK17:01 27 Jan '08  
GeneralRe: Project PinmemberDewey13:53 6 Feb '08  
GeneralRe: Project PinmemberMember 37794346:08 14 May '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 27 Jan 2008
Article Copyright 2008 by Tolgahan ALBAYRAK
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid