Wednesday, January 21, 2009

How to create a simple SEO Module in ASP.NET

Hey friends,
In the development life, we often develop asp.net pages that shows various category listings and product listings. Creating a page somewhat like http://yourDomain.Com/Categories.aspx?cid=123. As we all know, this kinda urls are not search engine friendly urls. The simplest way to make these urls search engine friendly is to use url rewrites.

There are lots and lots of url rewrites available across the web which converts above url to something like: http://yourDomain.com/cid/123/Categories.aspx or may be http://yourDomain.com/Categories123.htm. So that would make the url search engine friendly.

The next thing comes in SEO is to make proper entries for page's title, Description and keywords. We can set these things using meta tags. which looks something like



<meta content="Description of the content in the page" name="description"/>
<meta content="keywords related to the content" name="keywords"/>

So next thing comes is, how to make title, description and keywords different according to the querystring value of category in our above example. Well my approach to deal with this is:

  1. Create an Xml file which stores category id, title, description and keywords. Here is a sample xml that looks like:

    <?xml version="1.0" encoding="utf-8" ?>
    <category>
    <entry id="29" keywords="keywords for category 29" title="category name and some most relevant keywords" description="description about category"/>
    <entry id="31" keywords="keywords for category 31" title="category name and some most relevant keywords" description="description about category"/>
    <entry id="32" keywords="keywords for category 32" title="category name and some most relevant keywords" description="description about category"/>
    </category>


  2. Add asp.net literal control to header of the page. Please note that if you are using the master page, you should add this control to header tag of master page, otherwise you should add it to page's header. Another thing is, you should mark the header tag with runat=server to get this working

    <asp:literal runat="server" id="lMetaContainer"></asp:literal>


  3. Read the xml in page load event and find the relative category in xml node. Here a c# code for that is having a literal control in master page. As i know its very easy to work with the normal page.

    //using System.Xml;
    if (!IsPostBack)
    {
    if(Request.QueryString["cat"] !=null)
    {
    string cat = Request.QueryString["cat"].ToString();
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(Server.MapPath("~/CategoryData.xml"));
    //Use appropriate path in Server.MapPath
    XmlElement currentCat = null;
    XmlElement root = xmlDoc.DocumentElement;
    // i know this can be optimized by using
    // root.SelectSingleNode method, :) try it yourself
    foreach (XmlElement ele in root.ChildNodes)
    {
    if (ele.Attributes["id"].Value == cat)
    {
    currentCat = ele;
    break;
    }
    }
    if (currentCat != null)
    {
    string catTitle = currentCat.Attributes["title"].Value.ToString();
    string catMetaDesc = currentCat.Attributes["description"].Value.ToString();
    string catMetaKeywords = currentCat.Attributes["keywords"].Value.ToString();

    this.Page.Title = catTitle ;
    Literal lMeta = (Literal)this.Page.Header.FindControl("lMetaContainer");
    //tip:comment out above line if you are not using master page and replace lMeta with
    //your literal control"s id in the below code
    System.Text.StringBuilder meta = new System.Text.StringBuilder();
    meta.Append("<meta name=\"description\" content=\"" + catMetaDesc + "\" />");
    meta.Append("<meta name=\"keywords\" content=\"" + catMetaKeywords + "\" />");
    lMeta.Text = meta.ToString();
    }
    }
    }


Using xml in the module will make the implementation of the SEO separate. So the guy who writes SEO keywords, descriptions, and titles will get only xml to modify. If its not convinient to do so for an SEO guy, we can write a simple window base tool that will read and write the xml accordingly.

Also, we can extend this for multiple items, or make savaral xmls for each listings as required. Tell me what you think about this module so that we can improve this...

You can easily modify the code in whatever the way you want and implement the SEO friendly web pages in your website. Feel free to ask for more details or modifications.

Happy coding :)

No comments:

Post a Comment

Please add your valuable comments about this post if it helped you. Thanks

Popular Posts