I had previously created a method on my base controller:
public bool IsJsonRequest()
{
var acceptTypes = Request.AcceptTypes;
return acceptTypes != null &&
acceptTypes.Any(a => a.Equals("application/json",
StringComparison.OrdinalIgnoreCase));
}
Then I stumbled upon AjaxRequestExtensions.IsAjaxRequest()
. The documentation for it is very vague:
true if the specified HTTP request is an AJAX request; otherwise, false.
This got me thinking, what does it about an HTTP request that makes it AJAX? Is there more to it than checking the requester's accepted content types, or is encoding not a requirement of being 'AJAX'?
(Note that in my case my actions should only be called from JS pages on my site, and JSON is the only necessary encoding.)