How to find if the request is from Ajax in ASP.NET MVC?

It is easy to find out if the actual request came from Ajax or is it a normal request in ASP.NET MVC. In the controller, one can use the Request.IsAjaxRequest method to find this.

How to find if the request is from Ajax in ASP.NET MVC?

Below is a sample code snippet demonstrating how to find if the request is from Ajax in ASP.NET MVC?

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

namespace AbundantCodeMVC.Controllers

{

public class HomeController : Controller

{

//

// GET: /Home/

public ActionResult Index()

{

if (Request.IsAjaxRequest())

return PartialView();

return View();

}

}

}