我在_Layout.cshtml母版页中有两个元标记,现在我想在someone.cshtml视图页面中添加元标记。
我也试着用这个代码
放入_layout.cshtml母版页@RenderSection("metatags",false);
加入像someone.cshtml一样的@section metatags { <meta ... /> }
但没有获得成功。
还可以尝试使用add meta标记jquery,但效果并不好。
发布于 2014-04-22 06:12:01
我找到了一种暂时有效的方法。
此解决方案主要用于多个母版页。
在控制器/操作中,为该视图分配所需的任何元信息。
ViewBag.Title = "some title";
ViewBag.MetaDescription = "some description";在“视图”或“母版”页面中,获取该视图所需的任何元信息。
@if(ViewBag.MetaDescription != null)
{
<meta name="description" content="@ViewBag.MetaDescription" />
}
@if(ViewBag.MetaKeywords != null)
{
<meta name="keywords" content="@ViewBag.MetaKeywords" />
}发布于 2014-04-21 11:56:11
应该管用的。
这是我的主页_Layout.cshtml
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
@RenderSection("metatags", false)
<title>My ASP.NET Application</title>
</head>
<body>
@RenderBody()
</body>
</html>这是index.cshtml文件
@section metatags
{
<meta name="test" content="test"/>
<meta name="test2" content="test"/>
}
<div>Page content</div>结果
<html>
<head>
<meta charset="utf-8">
<meta name="test" content="test">
<meta name="test2" content="test">
<title>My ASP.NET Application</title>
</head>
<body>
<div>Page content</div>
</body>
</html>发布于 2014-04-22 11:46:26
如果使用嵌套布局,则需要遵循以下指导原则:
你所使用的技术应该有效,如果不是的话,也许这就是原因。
但是,看看您自己的答案中的预期用途,如果您只需要修改关键字和描述标记,那么NopCommrece中就有apis了。
在您的主布局中:
<meta name="description" content="@(Html.NopMetaDescription())" />
<meta name="keywords" content="@(Html.NopMetaKeywords())" />在客户端cshtml文件中
@{
Html.AddMetaDescriptionParts(Model.MetaDescription);
Html.AddMetaKeywordParts(Model.MetaKeywords);
}在NopCommerce代码中有大量的示例。
https://stackoverflow.com/questions/23196597
复制相似问题