locked
What s the best practice to use meta tags in a database driven website RRS feed

  • Question

  • User-1352156089 posted

    Hi All,

    I am trying to optimize the backend for my database driven website and I am kind of stuck on how to manage metatags.

    I have read many sources and approaches about this topic and still I don't feel I reached the best solution and some brainstorming with you guys would for sure help me understand better what is going to be the best approach to follow.

    I ll give you some insights about how I am managing my website: all the content of my website is managed dynamically from my backend: text, pictures, maps etc.  are all added to database and they automatically populate my website. This means that when I add, let's say a new continent, let's say Africa, a new dynamic page is added to the website in the form of: http://www.mysite.com/continents/africa

    In order to populate this new page with meta tags I am using the following code in masterpage:

    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Configuration
    Imports System.Web.UI.HtmlControls
    Partial Class admin_MasterPage
        Inherits System.Web.UI.MasterPage
        Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
            Dim page As String = Request.Url.Segments(Request.Url.Segments.Length - 2)
            Dim dtMeta As DataTable = Me.GetData(page)
            'Add Page Title
            Me.Page.Title = dtMeta.Rows(0)("Title").ToString()
            'Add Keywords Meta Tag
            Dim keywords As New HtmlMeta()
            keywords.HttpEquiv = "keywords"
            keywords.Name = "keywords"
            keywords.Content = dtMeta.Rows(0)("Keywords").ToString()
            Me.Page.Header.Controls.Add(keywords)
            'Add Description Meta Tag
            Dim description As New HtmlMeta()
            description.HttpEquiv = "description"
            description.Name = "description"
            description.Content = dtMeta.Rows(0)("Description").ToString()
            Me.Page.Header.Controls.Add(description)
        End Sub
    
        Private Function GetData(page As String) As DataTable
            Dim query As String = "SELECT Title, Description, Keywords FROM MetaTags WHERE LOWER(Page) = LOWER(@Page)"
            Dim constr As String = ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString
            Using con As New SqlConnection(constr)
                Using cmd As New SqlCommand(query)
                    Using sda As New SqlDataAdapter()
                        cmd.CommandType = CommandType.Text
                        cmd.Parameters.AddWithValue("@Page", page)
                        cmd.Connection = con
                        sda.SelectCommand = cmd
                        Dim dt As New DataTable()
                        sda.Fill(dt)
                        Return dt
                    End Using
                End Using
            End Using
        End Function
    End Class

    The meta tags are added correctly to every single page when I add into my database the last URL segment (africa in this case)  that has been created and populate the table with the description, key words and title.

    What I don't like in this approach:

    What would you suggest to implement/modify in order to solve the 2 above points?

    Thank you so much in advance

    Saturday, May 30, 2015 6:34 AM

Answers

  • User-84896714 posted

    Hi claudio7810,
    Thank you for your post.
    According to your description, I suggest you record all the segments of url in your page column.
    Code blow is for your reference.

    Dim page As String = Request.Url.AbsolutePath

    Hoping my reply could be helpful to you.
    Best Regards,
    Wang Li

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 1, 2015 5:26 AM