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

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

Lois said:

Wow this is great.

# September 26, 2010 2:31 PM

Soujanya said:

this is awesome explanation and code snippet for the beginners

# January 7, 2011 12:56 AM

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

cheap herve leger dress said:

I am so thrilled for having found your site.

# July 20, 2011 5:07 AM

Cheap Louis Vuitton Outlet said:

I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work. I am always trying to foster good relationships with people who can help my cause. This really breaks it down to a step by step process

# July 20, 2011 10:15 PM

hooher tod said:

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

# September 3, 2011 5:27 PM