.NET FAQs Unleashed!

 
 


    



    



Creating Search Engine Friendly Dynamic Web Pages in ASP.NET using C#

By Vishal Khanna

15th Sep 2008

With not much available on the Internet on how to dynamically create web pages in C# and publish them live instantly, I thought to write this down for fellow programmers who are in search of such an approach. There are N number of ways a requirement may be achieved, and this approach is one of them.

Goal: This article shall explain how to dynamically create SEO Friendly web pages in ASP.NET using C# as the programming language.

What needs to be there in a web page to be SEO Friendly? Well, this is a million billion trillion dollar question! The following points may be incorporated in the webpage:

  • Add Meta Tags for robots, keywords, description
  • Important page contents should be reflected in the URL name
  • JS & CSS file should be kept outside the webpage
  • Fill as much unique content as possible; Robots love only unique pages
  • Add a proper page Title

The list above cant ever be exhaustive, and it may go on and on, so let me leave it there and rather focus on the code.

Add following textboxes in the aspx page: txtUrl, txttitle, txtMetaContents, txtMetaKeywords, txtContents. Also, in the Page directive of this webpage, make sure you set ValidateRequest="false".

In the .cs file of your web page, add the following code:

using System.IO;

using System.Text;

// The dynamic file would be generated and published in a location called FolderLocation in the Root directory.

// In the event or function where you want to generate the page, use the following code:

public void fnCreatePage()

{

StreamWriter fp = new StreamWriter(Server.MapPath(".\\FolderLocation\\") + fnReturnHyphenSeparatedString(txtUrl.Text).Trim().ToString() + ".aspx");

//Note: txtUrl is a Textbox in the form for entering the URL information

fp.WriteLine("<%@ Page Language=\"C#\" MasterPageFile=\"~/MasterPage.master\" Title=\"" + txttitle.Text + "\" %>");

//Note: txttitle is a Textbox in the form for entering the Title information

//Add Meta Tags

fp.WriteLine("<script runat=\"Server\">");

fp.WriteLine("protected void Page_Load(object sender, EventArgs e)");

fp.WriteLine("{ //Setup the Meta Tag http-equiv");

fp.WriteLine("HtmlMeta htmlmeta0 = new HtmlMeta();");

fp.WriteLine("htmlmeta0.Name = \"robots\";");

fp.WriteLine("htmlmeta0.Content = \"index, follow\";");

fp.WriteLine("Page.Header.Controls.Add(htmlmeta0); ");

fp.WriteLine("HtmlMeta htmlmeta1 = new HtmlMeta();");

fp.WriteLine("htmlmeta1.HttpEquiv = \"Content-Type\";");

fp.WriteLine("htmlmeta1.Content = \"text/html; charset=iso-8859-1\";");

fp.WriteLine("Page.Header.Controls.Add(htmlmeta1);");

fp.WriteLine("HtmlMeta htmlmeta6 = new HtmlMeta();");

fp.WriteLine("htmlmeta6.Name = \"Title\";");

fp.WriteLine("htmlmeta6.Content = \"" + txtMetaContents.Text + "\";");

//Note: txtMetaContents is a Textbox in the form for entering the Meta Contents information

fp.WriteLine("Page.Header.Controls.Add(htmlmeta6);");

fp.WriteLine("HtmlMeta htmlmeta2 = new HtmlMeta();");

fp.WriteLine("htmlmeta2.Name = \"Description\";");

fp.WriteLine("htmlmeta2.Content = \"" + txtMetaDescription.Text + "\";");

//Note: txtMetaDescription is a Textbox in the form for entering the Meta Description information

fp.WriteLine("Page.Header.Controls.Add(htmlmeta2);");

fp.WriteLine("HtmlMeta htmlmeta3 = new HtmlMeta();");

fp.WriteLine("htmlmeta3.Name = \"Keywords\";");

fp.WriteLine("htmlmeta3.Content = \"" + txtMetaKeywords.Text + "\";");

//Note: txtMetaKeywords is a Textbox in the form for entering the Meta Keywords information

fp.WriteLine("Page.Header.Controls.Add(htmlmeta3);");

fp.WriteLine("} </script>");

fp.WriteLine("<br/>" + txtContents.Text);

//Note: txtContents is a Textbox in the form for entering the Contents information

fp.Close();

}

public string fnReturnHyphenSeparatedString(string strInput)

{

strInput = strInput.Replace(" ", "-");

return strInput;

}

Note that the generated page does not have a cs file. The complete code is inline.

The page generated above shall be completely search engine friendly. You also need to make sure that the folder "FolderLocation" needs to be accessible by the asp.net worker process. The code above also may be tweaked by you, any way you want to create your own sweet little content management system.

Happy Programming.

Cheers!