How to pass page's meta tags in ASP.NET MVC?

ciscocert picture ciscocert · Sep 27, 2008 · Viewed 7.1k times · Source

I'm playing with ASP.NET MVC for the last few days and was able to build a small site. Everything works great.

Now, I need to pass the page's META tags (title, description, keywords, etc.) via the ViewData. (i'm using a master page).

How you're dealing with this? Thank you in advance.

Answer

Ricky picture Ricky · Sep 27, 2008

Here is how I am currently doing it...

In the masterpage, I have a content place holder with a default title, description and keywords:

<head>
<asp:ContentPlaceHolder ID="cphHead" runat="server">
    <title>Default Title</title>
    <meta name="description" content="Default Description" />
    <meta name="keywords" content="Default Keywords" />
</asp:ContentPlaceHolder>
</head>

And then in the page, you can override all this content:

<asp:Content ID="headContent" ContentPlaceHolderID="cphHead" runat="server">
    <title>Page Specific Title</title>
    <meta name="description" content="Page Specific Description" />
    <meta name="keywords" content="Page Specific Keywords" />
</asp:Content>

This should give you an idea on how to set it up. Now you can put this information in your ViewData (ViewData["PageTitle"]) or include it in your model (ViewData.Model.MetaDescription - would make sense for blog posts, etc) and make it data driven.