Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
Hi everyone,

I am working on mvc2.

I want used Meta tags.
I am new on the meta tags and seo.
How can used meta tags on my page?
What is the best way to used meta tags on mvc?
Posted
Updated 2-Dec-10 11:53am
v2
Comments
Dalek Dave 2-Dec-10 17:53pm    
Edited for Readability.

Just put the meta tags in the view.
 
Share this answer
 
Comments
Dalek Dave 2-Dec-10 17:53pm    
Simple answer :)
XML
Put it in your viewdata! Do something like the following...

BaseViewData.cs - this is a viewdata class that all other viewdata classes will inherit from

public class BaseViewData
{
    public string Title { get; set; }
    public string MetaKeywords { get; set; }
    public string MetaDescription { get; set; }
}

Then your Site.Master (or whatever) class should be defined as follows:

public partial class Site : System.Web.Mvc.ViewMasterPage<BaseViewData>
{
}

Now in your Site.Master page simply have

<title><%=ViewData.Model.Title %></title>
<meta name="keywords" content="<%=ViewData.Model.MetaKeywords %>" />
<meta name="description" content="<%=ViewData.Model.MetaDescription %>" />

And you're away laughing!

HTHs, Charles

Ps. You can then expand on this idea, e.g. put a getter to your User (IPrincipal) Class into a LoggedInBaseViewData class.




Or simply you can Go like this
XML
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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900