【发布时间】:2011-08-04 08:03:39
【问题描述】:
使用 ASP.NET MVC 实现 Google 的 hashbang/Ajax 抓取模式的最佳实践是什么?
http://code.google.com/web/ajaxcrawling/docs/getting-started.html:
爬虫会修改每个 AJAX URL 比如
www.example.com/ajax.html#!key=value暂时变成
www.example.com/ajax.html?_escaped_fragment_=key=value
ASP.NET 的路由框架不允许指定查询字符串参数,但当然您始终可以创建一个将 _escaped_fragment_ 作为参数的操作方法(或者甚至只是在请求标头中查找 _escaped_fragment_ 参数)。
不过,这有点麻烦。有没有更好的办法?
更新:
我继续并实现了以下模式(在我的例子中,片段看起来像一个常规的 url 路径)。同样,这并不是最干净的方法,因此欢迎提出任何建议。
public virtual ActionResult Index(int id, string _escaped_fragment_)
{
//Handle Google Ajax Crawler
if (_escaped_fragment_ != null)
{
string[] fragments = _escaped_fragment_.Split(new char[]{'/'}, StringSplitOptions.RemoveEmptyEntries);
if (fragments.Length > 0)
{
//parse fragments
//return static content
}
}
//normal action operation
return View();
}
【问题讨论】:
-
您可能希望将其放在控制器的 OnActionExecuting 方法中,而不是在操作中。这样您就可以重定向到您认为最好的任何操作。
标签: asp.net-mvc ajax search routing