Asp.net mvc 3 剃须刀引擎-搜索引擎优化元标签
我需要放置唯一的描述和关键字元标签到每个页面。试过这个。但它不是点击Asp.net mvc 3 剃须刀引擎-搜索引擎优化元标签,asp.net-mvc-3,seo,razor,meta-tags,Asp.net Mvc 3,Seo,Razor,Meta Tags,我需要放置唯一的描述和关键字元标签到每个页面。试过这个。但它不是点击 @{ ViewBag.Title = "Title"; ViewBag.Description = "Test"; ViewBag.Keywords = "Test1, Test2, Test3"; } 如何在MVC 3 Razor Engine中放置元标记?您需要在布局页面中发出使用这些值的标记。 您可以通过写入@ViewBag.Description来获取标记中的值。您可以这样做,但必须将它们链接
@{
ViewBag.Title = "Title";
ViewBag.Description = "Test";
ViewBag.Keywords = "Test1, Test2, Test3";
}
如何在MVC 3 Razor Engine中放置元标记?您需要在布局页面中发出使用这些值的标记。您可以通过写入
@ViewBag.Description
来获取标记中的值。您可以这样做,但必须将它们链接到_Layout.cshtml中。将此添加到
部分的_Layout.cshtml中:
@if(ViewBag.Description!=null)
{
<meta name="description" content="@ViewBag.Description" />
}
@if(ViewBag.Keywords!=null)
{
<meta name="keywords" content="@ViewBag.Keywords" />
}
@if(ViewBag.Description!=null)
{
}
@if(ViewBag.Keywords!=null)
{
}
在布局中,您可以定义一个部分:
<head>
...
@RenderSection("metas")
</head>
我想至少投票三次。简单、清晰、切中要害。非常感谢,我使用了
ViewBag
,因为它处理null,并且在布局中只使用了@ViewBag.Title
,效果非常好,但是@RenderSection
如果Title为null,则抛出运行时未定义错误使用@if(ViewBag.Description!=null)的原因是什么
?因此,如果不需要,它不会添加标签。
@section metas
{
<meta name="description" content="Test" />
<meta name="title" content="Title" />
<meta name="keywords" content="Test1, Test2, Test3" />
...
}
<head>
<meta name="description" content="@ViewBag.Description" />
<meta name="title" content="@ViewBag.Title" />
<meta name="keywords" content="@ViewBag.Keywords" />
...
</head>
@{
ViewBag.Title = "Title";
ViewBag.Description = "Test";
ViewBag.Keywords = "Test1, Test2, Test3";
}