MonoRail ships with built-in support for the Prototype javascript framework. The AjaxHelper providers wrappers for generating the required Prototype code to perform asynchronous requests. Often times you will want to perform slightly different logic in your controller if the current request is an AJAX request (render a different view, cancel the layout, return some JSON serialized data, etc.). You can use the following method for a consistent way of determining if the current request is an AJAX request:
namespace My.App
{
using System;
using Castle.MonoRail.Framework;
public class ControllerBase : Controller
{
protected virtual bool IsAjaxRequest
{
get
{
string requestedWith = Request.Headers["X-Requested-With"];
if( string.IsNullOrEmpty(requestedWith) )
return false;
return requestedWith.Equals("XMLHttpRequest", StringComparison.InvariantCultureIgnoreCase);
}
}
}
}
This technique will also work for AJAX requests made using the jQuery framework. More on jQuery later.
posted @ Saturday, November 17, 2007 6:23 AM