How to Check if Request come from ajax in asp .net MVC



To Check if Request comes from ajax in Controller use the following :

public JsonResult GetEmployeeName( int id)
{
     if (Request.IsAjaxRequest())
      {
        
       }
}

or you can set [AjaxOnly] annotation to set action in controller works only for ajax
request.



[AjaxOnly]
public JsonResult GetEmployeeNameint id)
{
var result = Json(GetEmployeeByID(id)
, JsonRequestBehavior.AllowGet);
return result ;
}


EmoticonEmoticon