MVC 3 Razor 元描述/关键字
将元描述/关键字添加到我的 _Layout 页面上的最简单方法是什么? 类似于获取页面标题的方式会很好。比如
<title>@ViewBag.Title</title>
<meta name="keywords" content='@ViewBag.Keywords'/>
<meta name="description" content='@ViewBag.Description'/>
我的观点看起来像这样:
@{
ViewBag.Title = "Contact Me";
ViewBag.Keywords = "My, keyword, list, etc";
}
What is the simplest way of getting meta description / keywords onto my _Layout page?
Something similar to how you get the page Title would be nice. Such as
<title>@ViewBag.Title</title>
<meta name="keywords" content='@ViewBag.Keywords'/>
<meta name="description" content='@ViewBag.Description'/>
My view would look something like:
@{
ViewBag.Title = "Contact Me";
ViewBag.Keywords = "My, keyword, list, etc";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的视图不应该对此负责(不要向视图添加逻辑)。相反,应该指定元信息的是操作方法。
在这里检查我的答案:asp.net mvc - 包含 SEO 信息(例如元关键字和描述)的策略
答案仍然适用,尽管您应该使用
ViewBag
而不是ViewData
。Your view should not be responsible of that (do not add logic to views). Instead, it's the action method that should specify meta information.
Check my answer here: asp.net mvc - strategy for including SEO information such as meta keywords and descriptions
The answer still apply, although you should use
ViewBag
instead ofViewData
.我编写了一个自定义的 mvc3 元标记助手来实现这一点。当使用多种语言时,它可以与资源一起使用(如果需要):
然后在您的 _Layout.cshtml 文件中,您可以像这样使用它:
...
...
在您的资源文件中,您应该添加以下内容(Controller-> Home,Action-> Index):
mt_home -> Index主页标题
md_home ->主页描述在这里
mk_home -> 首页关键字
关于页面的
mt_home_about->关于标题
等等。
您还可以选择覆盖特定视图中资源的元数据,如下所示
索引.cshtml:
I wrote a custom mvc3 metatag helper that would do just that. It works with resources when using multiple languages (if required):
Then in your _Layout.cshtml file you use it like so:
...
...
In your resources file you should add the following (Controller-> Home, Action->Index):
mt_home -> homepage Title
md_home -> homepage description here
mk_home -> homepage keywords
for about page
mt_home_about -> about title
etc.
You also have an option to overwrite metadata from resources in specific views like so
Index.cshtml: