【问题标题】:Implementing Google's hashbang/Ajax crawl with ASP.NET MVC?使用 ASP.NET MVC 实现 Google 的 hashbang/Ajax 抓取?
【发布时间】: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


【解决方案1】:

您编写使用自定义模型绑定器,它将采用_escaped_fragment_ 查询字符串参数并返回一些强类型模型:

public ActionResult Index(MyModel model)
{
    // Directly use model.Id, model.Key1, model.Key2, ...
    return View();
}

【讨论】:

    猜你喜欢
    • 2012-07-22
    • 2014-05-30
    • 2012-11-26
    • 2013-04-12
    • 2023-04-03
    • 2020-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多