Razor Engine - SEO Meta tags

2019-01-31 23:44发布

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?

3条回答
2楼-- · 2019-01-31 23:55

You need to emit <meta> tags in your layout page that use the values.
You can get the values in the tags by writing @ViewBag.Description.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-02-01 00:03

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";
}
查看更多
女痞
4楼-- · 2019-02-01 00:12

You can do as you did, but you have to link them in your _Layout.cshtml. Add this to your _Layout.cshtml in the <head> section:

@if(ViewBag.Description!=null)
{
    <meta name="description" content="@ViewBag.Description" />
}
@if(ViewBag.Keywords!=null)
{
    <meta name="keywords" content="@ViewBag.Keywords" />
}
查看更多
登录 后发表回答