Razor Engine - SEO Meta tags

折月煮酒 提交于 2019-11-29 23:08:09

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";
}

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" />
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!