Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing Google's hashbang/Ajax crawl with ASP.NET MVC?

What are best practices for implementing Google's hashbang/Ajax crawl pattern with ASP.NET MVC?

http://code.google.com/web/ajaxcrawling/docs/getting-started.html:

the crawler will modify each AJAX URL such as

www.example.com/ajax.html#!key=value

to temporarily become

www.example.com/ajax.html?_escaped_fragment_=key=value

ASP.NET's Routing framework does not allow specifying query string parameters, but of course you can always create an action method that takes _escaped_fragment_ as a parameter (or even just look for the _escaped_fragment_ parameter in the request header).

That's a bit cumbersome however. Is there a better way?

UPDATE:

I went ahead and implemented the following pattern (in my case the fragments look like a regular url path). Again, this is hardly the cleanest approach, so any suggestions are welcome.

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();
}
like image 635
Oskar Austegard Avatar asked Apr 13 '11 00:04

Oskar Austegard


1 Answers

You write use a custom model binder which will take the _escaped_fragment_ query string parameter and return some strongly typed model:

public ActionResult Index(MyModel model)
{
    // Directly use model.Id, model.Key1, model.Key2, ...
    return View();
}
like image 125
Darin Dimitrov Avatar answered Oct 21 '22 18:10

Darin Dimitrov