手头的项目,需要进行SEO优化,就考虑做一个SEO Helper,使用Asp.net MVC Razor 视图引擎,真是轻松愉快:
1、首先,在Layout页面中Render一个Section,名为SEO,如下所示:
<head> <title>@ViewBag.Title</title> @RenderSection("SEO",false) ......
2、在App_Code中添加一个SEO.cshtml,代码如下:
@helper MetaTag(string description,string keywords) {<meta name="DESCRIPTION" content="@description" /> <meta name="KEYWORDS" content="@keywords" />} @helper NoIndex() {<meta name="robots" content="noindex" />}
3、在实现页面中,定义 SEO Section
@section SEO { @SEO.MetaTag("This is a description of my site." ,"this keyword,serperated by comma,another,keyword") }
这样浏览页面时,就可以发现 <meta name="description" content="...." /> 与 <meta name="keyword" content="...">已经被加入到页面中。
当然,如果不希望搜索引擎收录的页面,可已使用 @SEO.NoIndex() 写如一个不让收录的 <meta>