2

I wrote few partial methods in my controller and it is accessing directly typing in url. I want to restrict those particular methods.I used [ChildActionOnly] Attribute to restrict it. It works fine, but I am using Ajax Call to access those methods. The Ajax call is not working in ChildActionOnly. Is there any Attribute which will block from url and allows Ajax calls? Thanks.

1 Answers1

6

You may use the Request.IsAjaxRequest() to determine whether the call is an ajax call or not and do whatever you want to do.

public ActionResult TestRadioButtons()
{
     if(!Request.IsAjaxRequest())
       return new HttpNotFoundResult("Not found");

    //continue processing 
   // to do :Return something    
}

If you like, you may simply redirect user to another page (non ajax action method) instead of returning NotFound result.

if(!Request.IsAjaxRequest())
{
   return RedirectToAction("Index");
}
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • This is an excellent work-around, should be marked as correct by the OP! Helped me a lot today thank you! – Dan Scott Jul 25 '18 at 09:30