Nikolaos Kantzelis ASP.Net Blog

This blog will focus on ASP.NET Framework

Sponsors

About Me

Great Blogs

ASP.NET 4.0 meta tags and Search engine optimisation

I am thinking to create a new series of posts regarding ASP.NET and SEO (Search Engine Optimisation).

I am going to start with this post , talking about some new features that make our asp.net apps more SEO friendly.

At the end of the day, there is no point having a great application and somehow "scare" the search engines away. This is going to be a short post so let's quickly have a look at meta keywords and ASP.NET 4.0. Meta keywords and description are important elements of a page and make it search engine friendly. ASP.Net 4.0 added 2 new properties on the Page object to let us define the Meta Keywords and Description. Create a simple asp.net application using Visual Studio 2010. In the Default.aspx.cs code behind file type:

Page.MetaKeywords = "asp.net,vb,c#,css,html,"; Page.MetaDescription = "This is my blog that focuses on ASP.NET.";
Alternatively we can add those two meta tags in the Page directive
<%@ Page Language="C#" MetaKeywords="asp.net,vb,c#,css,html" MetaDescription="This is my blog that focuses on ASP.NET." AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
Run your application. Go to View->Source and see the meta tags
</title><meta name="description" content="This is my blog that focuses on ASP.NET." /><meta name="keywords" content="asp.net,vb,c#,css,html" /></head>
 
Now we can demonstrate the use of meta keywords in another example.
We will have a simple asp.net application where someone selects the categories from the dropdown list, and the relevant products appear in the Gridview control.
We will try to see how to make those dynamically created pages more SEO friendly.
I assume that you have access to a version of SQL Server and Northwind database.
If you do not, you can download and install the free SQL Server Express edition from here. If you need the installation scripts for the sample Northwind database, click here
1) Fire the VS 2010
2) Create a new website and use C# as the development language
3) Drag and drop a Gridview control from the Toolbox in the default.aspx page. Leave the default name.
4) Drag and drop a Dropdown control from the Toolbox in the default.aspx page. Leave the default name.
5) Drag and drop a SqlDataSource control from the Toolbox in the default.aspx page. Leave the the default name (SqlDataSource1).
6) Drag and drop a SqlDataSource control from the Toolbox in the default.aspx page. Leave the the default name (SqlDataSource2).
7) Configure the SqlDataSource2 control to use a connection string and open a connection string to the Northwind database.
The SQL statement should be "SELECT DISTINCT [CategoryName], [CategoryID] FROM [Categories]"
8) For the Dropdownlist1 control, set the datasource to the SqlDataSource2 and enable the AutoPostBack
9) Configure the SqlDataSource1 control to use a connection string and open a connection string to the Northwind database.
The SQL Statement should be something like this "SELECT CategoryID, ProductName FROM Products WHERE (CategoryID = @categoryid)"
Naturally we have a parameter, the categoryid is passed to the above query from the dropdownlist control.
10)  For the GridView1 control, set the datasource to the SqlDataSource1.
11) In the Page_Load event method type the following
protected void Page_Load(object sender, EventArgs e)
    {
       

            string myvalue = Convert.ToString(DropDownList1.SelectedItem);

            switch (myvalue)
            {
                case "Beverages":
                    Page.MetaDescription = "display products that belong to beverages";
                    Page.MetaKeywords = "beverages,refreshements";
                    break;
                case "Condiments":
                    Page.MetaDescription = "display products that belong to condiments";
                    Page.MetaKeywords = "condiments";
                    break;
                case "Confections":
                    Page.MetaDescription = "display products that belong to confections";
                    Page.MetaKeywords = "confections";
                    break;
                case "Dairy Products":
                    Page.MetaDescription = "display products that belong to dairy products";
                    Page.MetaKeywords = "dairy products";
                    break;
                case "Grains/Cereals":
                    Page.MetaDescription = "display products that belong to grains/cereals";
                    Page.MetaKeywords = "grains";
                    break;
                case "Meat/Poultry":
                    Page.MetaDescription = "display products that belong to meat/poultry";
                    Page.MetaKeywords = "meat/poultry";
                    break;
                case "Produce":
                    Page.MetaDescription = "display products that belong to produce";
                    Page.MetaKeywords = "produce";
                    break;
                case "Seafood":
                    Page.MetaDescription = "display products that belong to seafood";
                    Page.MetaKeywords = "seafood";
                    break;
                default:
                    Page.MetaDescription = "Welcome to our products sites";
                    Page.MetaKeywords = "beverages,condiments,seafood";
                    break;
            }
               

You can see what I am doing here. I just find the selected value that the user selected and display the relevant meta keywords that I want.
12) Run your application and make sure everything works ok. Then go to your browser window and select View->Source and see the meta keywords for all the various selections of categories you make.  
Leave a comment with your email of you need the source code.
Hope it helps!!!

Comments

ASP.NET 4.0 meta tags and Search engine optimisation - Nikolaos Kantzelis-ASP.NET MVP said:

Pingback from  ASP.NET 4.0 meta tags and Search engine optimisation - Nikolaos Kantzelis-ASP.NET MVP

# March 23, 2010 9:11 AM

webbes said:

That's an awful lot of code! How about this:

string selectedText = DropDownList1.SelectedItem.Text;

Page.MetaDescription = "display products that belong to " +

                      selectedText.ToLower();

# March 25, 2010 12:00 PM

alireza.riazati said:

hi, please send me more about seo code samples...

very thank you.

# August 20, 2010 12:44 AM

Lois said:

Wow this is great.

# September 26, 2010 2:31 PM

Nikolaos Kantzelis-ASP.NET MVP said:

In this blog post I would like to talk about a new ASP.Net 4.0 feature, URL Routing . I know this issue

# January 3, 2011 11:39 AM

Soujanya said:

this is awesome explanation and code snippet for the beginners

# January 7, 2011 12:56 AM

Mohesh Bhattacharjee said:

First, thanks a lot. I am new in ASP.net tech and I found this is very useful to me. I also used it in my website.

http://www.moseysoftware.com

# March 29, 2011 6:46 PM

that's goode said:

it's good. meta tags problem is solved in asp.net 4.0 version

# April 3, 2011 7:33 AM

Ali said:

This is really helpful for beginners , please send me whole code. and can you give more examples on ASP.NET and SEO.

# April 21, 2011 7:15 AM

sharad said:

you did not add my comment regarding "the way of storing article page on server". no problem.

Anyhow, your article is very useful.

Good luck.

# June 1, 2011 11:53 AM

hooher tod said:

Yes there should realize the reader to RSS my feed to RSS commentary, quite simply

# September 3, 2011 5:27 PM

nakedcelebritiesh said:

looks of  <a href=www.tucp.org.ph/.../index.php naked pictures</a> nakedpictures .

# November 25, 2011 10:29 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)