robbanp
I'm the co-founder of this website and the tech lead. Follow me on: twitter.com/robertpohl
Blog

The Rob blog

I'm Robert Pohl, the creator and co-founder to ThatsToday. I blog mostly about technology and internet related topics. Follow me on Twitter @robertpohl
Subscribe to RSS

I’m working on a small ASP.NET website that require for some pages to have its own meta data keywords, description and title, so that they will be better indexed by search engines. This is also know as Search Engine Optimization (SEO).  The challenge here is to have the meta data information in the Master Page, and still be able to update the data from each page, and also do this without server side code on the different aspx pages.

The solution is to add a Content Place Holder called MetaData in your Master Page, and some logic to find and parse the data on each aspx page:

Master Page aspx:

<title id="MasterTitle" runat="server">Default title</title>
<meta name="description" id="MasterDescription" runat="server" content="Default description" />
<meta name="keywords" id="MasterKeywords" runat="server" content="default, description," />

 

<asp:ContentPlaceHolder ID="MetaData" Visible="false" runat="server" />

 

Master Page cs:

protected void Page_Load(object sender, EventArgs e)

        {
            var keyWords = MetaData.FindControl("PageKeywords") as Literal;
            if( keyWords != null)
            {
                MasterKeywords.Attributes["content"] = keyWords.Text;
            }

            var description = MetaData.FindControl("PageDescription") as Literal;
            if (description != null)
            {
                MasterDescription.Attributes["content"] = description.Text;
            }

            var title = MetaData.FindControl("PageTitle") as Literal;
            if (title != null)
            {
                MasterTitle.InnerText = title.Text;
            }
        }

And in the Default aspx:

<asp:Content ID="metaData" ContentPlaceHolderID="MetaData" runat="server">
    <asp:Literal ID="PageTitle" runat="server">Title from page</asp:Literal> 
    <asp:Literal ID="PageDescription" runat="server">Description from page</asp:Literal>
    <asp:Literal ID="PageKeywords" runat="server">Keywords, from, page</asp:Literal>
</asp:Content>

 

In the Master Page you need to have the title, keywords and description run at server so you can update them from the code behind. In the Page Load there is a check to see if the Literals exist in the content place holder (which is hidden from rendering), and if so, update the tags with the Literal information.

Now my front end guy, can choose to update the meta data or title if it is needed, or just settle with the default information.

Comments on this article

Add your comment
Sign In

Not a member yet?

Signing up is FREE and will only take 15 seconds!

Facebook Login

Sign In

E-mail address:
Password:
Remember me
Sponsored links

Close