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


This content was proof-read by Grammarly. You can Try Grammary for FREE

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();

}

}

}
%d bloggers like this: