Razor Engine - SEO Meta tags

Aseem Gautam picture Aseem Gautam · Apr 3, 2011 · Viewed 16.3k times · Source

I need to place unique description and keywords meta tags to each page. Tried this. But its not clicking.

@{
    ViewBag.Title = "Title";
    ViewBag.Description = "Test";
    ViewBag.Keywords = "Test1, Test2, Test3";
}

How do I place the meta tags in MVC 3 Razor Engine?

Answer

Darin Dimitrov picture Darin Dimitrov · Apr 3, 2011

In the layout you could define a section:

<head>
    ...
    @RenderSection("metas")
</head>

and in view:

@section metas
{
    <meta name="description" content="Test" />
    <meta name="title" content="Title" />
    <meta name="keywords" content="Test1, Test2, Test3" />
    ...
}

or in the layout:

<head>
    <meta name="description" content="@ViewBag.Description" />
    <meta name="title" content="@ViewBag.Title" />
    <meta name="keywords" content="@ViewBag.Keywords" />
    ...
</head>

and in the view:

@{
    ViewBag.Title = "Title";
    ViewBag.Description = "Test";
    ViewBag.Keywords = "Test1, Test2, Test3";
}