logo
  • Home
  • My Works
  • Downloads
  • Personal
  • Guest Book
  • About Me

Search Engine Optimization in ASP.NET – How to implement

Published by Rafeeque B Muhammad on Saturday, January 30, 2010 12:00 AM
Updated on Saturday, January 30, 2010 12:00 AM


·         Dynamic Meta tag keywords

We can have HtmlMeta control in ASP.NET to generate Meta tag dynamically.

HtmlMeta meta = new HtmlMeta();

      meta.Name = "Keywords";

meta.Content = MetaDescription;      Page.Header.Controls.Add(meta);

·         Dynamic titles

Also we can have the dynamic titles, make the title as server control and use it in your code behind.

<title id="PageTitle" runat="server"> Some text </title>

PageTitle.Text = “Some text”;

·         Url rewriter with SEO friendly urls

 

I will try to explain the things we need to follow.

 

Ø  Create a class library project and implement the code given below. The main thing the logic on rewrite path. I have one ArticleDisplay.aspx page which will accept only the query string (article name). Here what I did is, I have rendered the with some custom format,

article- (hardcoded value)

articlename (created dynamically)

custom title (heading generated dynamically)

public class MyHttpModule : IHttpModule

    {

 

        #region IHttpModule Members

 

        public void Dispose()

        {

        }

 

        public void Init(HttpApplication context)

        {          

            context.AuthorizeRequest += new

               EventHandler(this.BaseModuleRewriter_AuthorizeRequest);

        }

 

        #endregion

 

        protected virtual void BaseModuleRewriter_AuthorizeRequest(

          object sender, EventArgs e)

        {

            HttpApplication app = (HttpApplication)sender;

            Rewrite(app);

        }

 

        private void Rewrite(System.Web.HttpApplication app)

        {

            string oldPath = app.Request.Url.ToString();

            string token = "article-";

            int i = oldPath.IndexOf(token);

 

            if (i > -1)

            {

                int underScoreIndex = oldPath.IndexOf("_");

                int startIndex = (i + token.Length);

                int numberOfCharacters = underScoreIndex - startIndex;

                string articleName = oldPath.Substring(startIndex, numberOfCharacters);

                app.Context.RewritePath("~/ArticleDisplay.aspx?Article=" + articleName);

            }

 

        }

 

    }

 

Ø  Generate the customized url . If you are using “/” to separate the url, may face a lot of issues  while implementing like if you have any subdirectory with some

 

<asp:Repeater ID="rptArticleList" runat="server">

           <ItemTemplate>                                               

                                            <tr>

                                            <a href="<%# GenerateSEOurl(DataBinder.Eval(Container.DataItem,"FileName"),DataBinder.Eval(Container.DataItem, "ArticleHeading")) %>">

                                            <%# "<strong>" +  DataBinder.Eval(Container.DataItem, "ArticleHeading") + "</strong>" %></a>

                                            </td>

                                            </tr>

                                            </ItemTemplate> 

 

    </asp:Repeater>

Ø  Public method to generate customized url string, just put this code in code behind. If you will use .html extension you may get error in your hosting server, the reason is IIS will not transfer your request to asp.net engine which has .html extension. If you have your own IIS then it’s ok or you have to handle the request at IIS level. This case will work at your local but doesn’t at hosting site.

 

public string GenerateSEOurl(object FileName, object Heading)

    {

        string seoFriendlyUrl = string.Empty;

        seoFriendlyUrl = "article-" + FileName + "_" + Heading.ToString().ToLower().Replace(' ','-') + ".aspx";

        return seoFriendlyUrl;

    }

 

Ø  Also need the config entry in web config, code given below

<httpModules>

      <add type="Full Name Space,Assembly name” name="some name" />

    </httpModules>

Technical Articles

  • All
  • .NET Framework
  • ASP.NET
  • C#
  • Win Forms
  • WCF
  • SharePoint(WSS 3.0)
  • SQL Server
  • TSQL
  • Oracle
  • PL/SQL
  • Performance and Tuning
  • Application and Tools
  • Outlook programming
  • HTML/HTML5/XHTML/DHTML/JAVAScript/CSS
  • Windows Services
  • Web Services
  • Security
  • Web
  • General
  • Architecture

Technical Books

  • All
  • C# Threading
  • WCF

Other Links

  • Important Links

Techies Craft © 2014 | Privacy Policy